From: Borislav Petkov <bp@alien8.de>
To: Xiongfeng Wang <wangxiongfeng2@huawei.com>
Cc: mchehab@kernel.org, tony.luck@intel.com,
linux-edac@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] EDAC/mc_sysfs: Add missing newlines when printing {max,dimm}_location
Date: Thu, 17 Sep 2020 18:25:37 +0200 [thread overview]
Message-ID: <20200917162537.GL31960@zn.tnic> (raw)
In-Reply-To: <591e613e-0b53-028f-08fd-3d62a35b8c4f@huawei.com>
On Thu, Sep 17, 2020 at 07:38:57PM +0800, Xiongfeng Wang wrote:
> I am not sure if snprintf will return a value larger than its second input
> paramter 'size'.
The comment over snprintf() says
* The return value is the number of characters which would be
* generated for the given input, excluding the trailing null,
* as per ISO C99. If the return is greater than or equal to
* @size, the resulting string is truncated.
And let's try it, see diff at the end. Now look what that produces:
[ 2.594796] kernel_init: len: 16, str: [A lo]
it returns 16 for len even though the buffer is 5 chars long. So in our
patch, we'd increment by 16 which would be wrong.
Now let's use scnprintf():
[ 2.700142] kernel_init: len: 4, str: [A lo]
Much better. Lemme do that.
> Not sure whether we need to check 'len' equals to 0.
> if (len <= 0) ?
Yeah, lemme fix that too, so we have now incrementally ontop:
---
diff --git a/drivers/edac/edac_mc_sysfs.c b/drivers/edac/edac_mc_sysfs.c
index fa0551c81e63..c56e0004b39e 100644
--- a/drivers/edac/edac_mc_sysfs.c
+++ b/drivers/edac/edac_mc_sysfs.c
@@ -822,18 +822,17 @@ static ssize_t mci_max_location_show(struct device *dev,
int i, n;
for (i = 0; i < mci->n_layers; i++) {
- n = snprintf(p, len, "%s %d ",
- edac_layer_name[mci->layers[i].type],
- mci->layers[i].size - 1);
-
+ n = scnprintf(p, len, "%s %d ",
+ edac_layer_name[mci->layers[i].type],
+ mci->layers[i].size - 1);
len -= n;
- if (len < 0)
+ if (len <= 0)
goto out;
p += n;
}
- p += snprintf(p, len, "\n");
+ p += scnprintf(p, len, "\n");
out:
return p - data;
}
---
Test diff:
---
diff --git a/init/main.c b/init/main.c
index ae78fb68d231..e2d6110d3a3d 100644
--- a/init/main.c
+++ b/init/main.c
@@ -1397,7 +1397,8 @@ void __weak free_initmem(void)
static int __ref kernel_init(void *unused)
{
- int ret;
+ char str[5];
+ int ret, len;
kernel_init_freeable();
/* need to finish all async __init code before freeing the memory */
@@ -1419,6 +1420,11 @@ static int __ref kernel_init(void *unused)
do_sysctl_args();
+ len = snprintf(str, 5, "A longer string\n");
+
+ pr_info("%s: len: %d, str: [%s]\n",
+ __func__, len, str);
+
if (ramdisk_execute_command) {
ret = run_init_process(ramdisk_execute_command);
if (!ret)
--
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquette
next prev parent reply other threads:[~2020-09-17 16:25 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-09-14 2:48 [PATCH v2] EDAC/mc_sysfs: Add missing newlines when printing {max,dimm}_location Xiongfeng Wang
2020-09-16 17:00 ` Borislav Petkov
2020-09-17 11:38 ` Xiongfeng Wang
2020-09-17 15:28 ` Joe Perches
2020-09-17 16:25 ` Borislav Petkov [this message]
2020-09-18 2:37 ` Xiongfeng Wang
2020-09-18 7:12 ` Borislav Petkov
2020-09-18 13:31 ` Joe Perches
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200917162537.GL31960@zn.tnic \
--to=bp@alien8.de \
--cc=linux-edac@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mchehab@kernel.org \
--cc=tony.luck@intel.com \
--cc=wangxiongfeng2@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).