2021-10-13 03:31:50

by 王擎

[permalink] [raw]
Subject: [PATCH] btrfs: replace snprintf in show functions with sysfs_emit

coccicheck complains about the use of snprintf() in sysfs show functions.

Fix the following coccicheck warning:
fs/btrfs/sysfs.c:335:8-16: WARNING: use scnprintf or sprintf.

Use sysfs_emit instead of scnprintf or sprintf makes more sense.

Signed-off-by: Qing Wang <[email protected]>
---
fs/btrfs/sysfs.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c
index 9d1d140..fda094a 100644
--- a/fs/btrfs/sysfs.c
+++ b/fs/btrfs/sysfs.c
@@ -332,7 +332,7 @@ BTRFS_ATTR(static_feature, supported_checksums, supported_checksums_show);
static ssize_t send_stream_version_show(struct kobject *kobj,
struct kobj_attribute *ka, char *buf)
{
- return snprintf(buf, PAGE_SIZE, "%d\n", BTRFS_SEND_STREAM_VERSION);
+ return sysfs_emit(buf, "%d\n", BTRFS_SEND_STREAM_VERSION);
}
BTRFS_ATTR(static_feature, send_stream_version, send_stream_version_show);

--
2.7.4


2021-10-13 07:54:22

by Anand Jain

[permalink] [raw]
Subject: Re: [PATCH] btrfs: replace snprintf in show functions with sysfs_emit

On 13/10/2021 11:28, Qing Wang wrote:
> coccicheck complains about the use of snprintf() in sysfs show functions.

It looks like the reason is snprintf() unaware of the PAGE_SIZE
max_limit of the buf.

> Fix the following coccicheck warning:
> fs/btrfs/sysfs.c:335:8-16: WARNING: use scnprintf or sprintf.

Hm. We use snprintf() at quite a lot more places in sysfs.c and, I don't
see them getting this fix. Why?

> Use sysfs_emit instead of scnprintf or sprintf makes more sense.

Below commit has added it. Nice.

commit 2efc459d06f1630001e3984854848a5647086232
Date: Wed Sep 16 13:40:38 2020 -0700

sysfs: Add sysfs_emit and sysfs_emit_at to format sysfs out

Thanks, Anand

>
> Signed-off-by: Qing Wang <[email protected]>
> ---
> fs/btrfs/sysfs.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c
> index 9d1d140..fda094a 100644
> --- a/fs/btrfs/sysfs.c
> +++ b/fs/btrfs/sysfs.c
> @@ -332,7 +332,7 @@ BTRFS_ATTR(static_feature, supported_checksums, supported_checksums_show);
> static ssize_t send_stream_version_show(struct kobject *kobj,
> struct kobj_attribute *ka, char *buf)
> {
> - return snprintf(buf, PAGE_SIZE, "%d\n", BTRFS_SEND_STREAM_VERSION);
> + return sysfs_emit(buf, "%d\n", BTRFS_SEND_STREAM_VERSION);
> }
> BTRFS_ATTR(static_feature, send_stream_version, send_stream_version_show);
>
>

2021-10-13 10:39:37

by David Sterba

[permalink] [raw]
Subject: Re: [PATCH] btrfs: replace snprintf in show functions with sysfs_emit

On Wed, Oct 13, 2021 at 03:51:33PM +0800, Anand Jain wrote:
> On 13/10/2021 11:28, Qing Wang wrote:
> > coccicheck complains about the use of snprintf() in sysfs show functions.
>
> It looks like the reason is snprintf() unaware of the PAGE_SIZE
> max_limit of the buf.
>
> > Fix the following coccicheck warning:
> > fs/btrfs/sysfs.c:335:8-16: WARNING: use scnprintf or sprintf.
>
> Hm. We use snprintf() at quite a lot more places in sysfs.c and, I don't
> see them getting this fix. Why?

I guess the patch is only addressing the warning for snprintf, reading
the sources would show how many more conversions could have been done of
scnprintf calls.

> > Use sysfs_emit instead of scnprintf or sprintf makes more sense.
>
> Below commit has added it. Nice.
>
> commit 2efc459d06f1630001e3984854848a5647086232
> Date: Wed Sep 16 13:40:38 2020 -0700
>
> sysfs: Add sysfs_emit and sysfs_emit_at to format sysfs out

The conversion to the standard helper is good, but should be done
in the entire file.

2021-10-13 10:52:18

by Qu Wenruo

[permalink] [raw]
Subject: Re: [PATCH] btrfs: replace snprintf in show functions with sysfs_emit



On 2021/10/13 18:36, David Sterba wrote:
> On Wed, Oct 13, 2021 at 03:51:33PM +0800, Anand Jain wrote:
>> On 13/10/2021 11:28, Qing Wang wrote:
>>> coccicheck complains about the use of snprintf() in sysfs show functions.
>>
>> It looks like the reason is snprintf() unaware of the PAGE_SIZE
>> max_limit of the buf.
>>
>>> Fix the following coccicheck warning:
>>> fs/btrfs/sysfs.c:335:8-16: WARNING: use scnprintf or sprintf.

IIRC sprintf() is less safe than snprintf().
Is the check really correct to mention sprintf()?

>>
>> Hm. We use snprintf() at quite a lot more places in sysfs.c and, I don't
>> see them getting this fix. Why?
>
> I guess the patch is only addressing the warning for snprintf, reading
> the sources would show how many more conversions could have been done of
> scnprintf calls.
>
>>> Use sysfs_emit instead of scnprintf or sprintf makes more sense.
>>
>> Below commit has added it. Nice.
>>
>> commit 2efc459d06f1630001e3984854848a5647086232
>> Date: Wed Sep 16 13:40:38 2020 -0700
>>
>> sysfs: Add sysfs_emit and sysfs_emit_at to format sysfs out
>
> The conversion to the standard helper is good, but should be done
> in the entire file.
>

Yeah, the same idea, all sysfs interface should convert to the new
interface, not only the snprintf().

Thanks,
Qu