btrfs: replace snprintf in show functions with sysfs_emit
PATCH 1/1 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.
--- fs/btrfs/sysfs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/sysfs.c +1 -1
@@ -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);
Signature · 1 line
2.7.4
Quoted 1 line · Qing Wang
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.
Quoted 2 lines
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?
Quoted 1 line
Use
sysfs_emitinstead 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
Quoted 20 lines
Signed-off-by Qing Wang <[email protected]>--- fs/btrfs/sysfs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/btrfs/sysfs.c +1
-1@@ -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);
Quoted 11 lines · Anand Jain
Quoted 1 line · Qing Wang
coccicheck complains about the use of
snprintf()in sysfs show functions.It looks like the reason is
snprintf()unaware of thePAGE_SIZEmax_limitof the buf.Quoted 2 lines
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 insysfs.cand, 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.
Quoted 8 lines
Quoted 1 line
Use
sysfs_emitinstead of scnprintf or sprintf makes more sense.Below commit has added it. Nice.
commit
2efc459d06f1630001e3984854848a5647086232
Date: Wed Sep 16 13:40:38 2020 -0700sysfs: Add
sysfs_emitandsysfs_emit_atto format sysfs out
The conversion to the standard helper is good, but should be done in the entire file.
Quoted 9 lines · David Sterba
Quoted 8 lines · Anand Jain
Quoted 1 line · Qing Wang
coccicheck complains about the use of
snprintf()in sysfs show functions.It looks like the reason is
snprintf()unaware of thePAGE_SIZEmax_limitof the buf.Quoted 2 lines
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()?
Quoted 12 lines
Quoted 3 lines
Hm. We use
snprintf()at quite a lot more places insysfs.cand, 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.
Quoted 4 lines
Quoted 1 line
Use
sysfs_emitinstead of scnprintf or sprintf makes more sense.Below commit has added it. Nice.
Quoted 8 lines
Quoted 4 lines
commit
2efc459d06f1630001e3984854848a5647086232
Date: Wed Sep 16 13:40:38 2020 -0700sysfs: Add
sysfs_emitandsysfs_emit_atto format sysfs outThe 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
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 5 lines
Quoted 5 lines · Anand Jain
Quoted 3 lines · Qing Wang
Quoted 1 line
coccicheck complains about the use of
snprintf()in sysfs show functions.
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
It looks like the reason is
snprintf()unaware of thePAGE_SIZEmax_limitof the buf.Quoted 2 lines
Fix the following coccicheck warning:
fs/btrfs/sysfs.c:335:8-16: WARNING: use scnprintf or sprintf.IIRC
sprintf()is less safe thansnprintf().
Is the check really correct to mentionsprintf()?
device_attr_show.cocci metions show() must not use snprintf() when formatting the value to be returned to user space. If you can guarantee that an overflow will never happen you can use sprintf() otherwise you must use scnprintf().
My understanding is this is not only to solve the possible overflow issue, snprintf() returns the length of the string, not the length actually written. We can directly use sysfs_emit() here.
Thanks,
Qing
Quoted 26 lines
Quoted 12 lines
Quoted 3 lines
Hm. We use
snprintf()at quite a lot more places insysfs.cand, 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.
Quoted 4 lines
Quoted 1 line
Use
sysfs_emitinstead of scnprintf or sprintf makes more sense.Below commit has added it. Nice.
Quoted 8 lines
Quoted 4 lines
commit
2efc459d06f1630001e3984854848a5647086232
Date: Wed Sep 16 13:40:38 2020 -0700sysfs: Add
sysfs_emitandsysfs_emit_atto format sysfs outThe 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
Quoted 11 lines · 王擎
Quoted 11 lines
Quoted 10 lines · Anand Jain
Quoted 8 lines · Qing Wang
Quoted 1 line
coccicheck complains about the use of
snprintf()in sysfs show functions.It looks like the reason is
snprintf()unaware of thePAGE_SIZEmax_limitof the buf.Quoted 2 lines
Fix the following coccicheck warning:
fs/btrfs/sysfs.c:335:8-16: WARNING: use scnprintf or sprintf.
Quoted 7 lines
Quoted 2 lines
IIRC
sprintf()is less safe thansnprintf().
Is the check really correct to mentionsprintf()?
device_attr_show.coccimetionsshow()must not usesnprintf()when formatting the value to be returned to user space. If you can guarantee that an overflow will never happen you can usesprintf()otherwise you must usescnprintf().
I totally understand snprintf() has its problem for not returning the real written size, thus not safe.
But sprintf() is worse, it doesn't even prevent overflow from the beginning.
In fact, for case that could overflow, snprintf() would only overflow if we have extra bytes to output and doesn't check if the offset is beyond PAGE_SIZE at snprintf() call.
But for sprintf(), it would cause overflow immediately.
Thus mentioning sprintf() is more problematic.
Only scnprintf() is safe.
But sure, sysfs_emit() and sysfs_emit_at() would be a better solution.
Thanks,
Qu
Quoted 35 lines
My understanding is this is not only to solve the possible overflow issue,
snprintf()returns the length of the string, not the length actually written. We can directly usesysfs_emit()here.Thanks,
Qing
Quoted 26 lines
Quoted 12 lines
Quoted 3 lines
Hm. We use
snprintf()at quite a lot more places insysfs.cand, 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.
Quoted 4 lines
Quoted 1 line
Use
sysfs_emitinstead of scnprintf or sprintf makes more sense.Below commit has added it. Nice.
Quoted 8 lines
Quoted 4 lines
commit
2efc459d06f1630001e3984854848a5647086232
Date: Wed Sep 16 13:40:38 2020 -0700sysfs: Add
sysfs_emitandsysfs_emit_atto format sysfs outThe 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