Skip to content

Commit 68ea60a

Browse files
zhijianli88JuliaLawall
authored andcommitted
coccinelle: device_attr_show: Adapt to the latest Documentation/filesystems/sysfs.rst
Adapt description, warning message and MODE=patch according to the latest Documentation/filesystems/sysfs.rst: > show() should only use sysfs_emit() or sysfs_emit_at() when formatting > the value to be returned to user space. After this patch: When MODE=report, $ make coccicheck COCCI=scripts/coccinelle/api/device_attr_show.cocci M=drivers/hid/hid-picolcd_core.c MODE=report <...snip...> drivers/hid/hid-picolcd_core.c:304:8-16: WARNING: please use sysfs_emit or sysfs_emit_at drivers/hid/hid-picolcd_core.c:259:9-17: WARNING: please use sysfs_emit or sysfs_emit_at When MODE=patch, $ make coccicheck COCCI=scripts/coccinelle/api/device_attr_show.cocci M=drivers/hid/hid-picolcd_core.c MODE=patch <...snip...> diff -u -p a/drivers/hid/hid-picolcd_core.c b/drivers/hid/hid-picolcd_core.c --- a/drivers/hid/hid-picolcd_core.c +++ b/drivers/hid/hid-picolcd_core.c @@ -255,10 +255,12 @@ static ssize_t picolcd_operation_mode_sh { struct picolcd_data *data = dev_get_drvdata(dev); - if (data->status & PICOLCD_BOOTLOADER) - return snprintf(buf, PAGE_SIZE, "[bootloader] lcd\n"); - else - return snprintf(buf, PAGE_SIZE, "bootloader [lcd]\n"); + if (data->status & PICOLCD_BOOTLOADER) { + return sysfs_emit(buf, "[bootloader] lcd\n"); + } + else { + return sysfs_emit(buf, "bootloader [lcd]\n"); + } } static ssize_t picolcd_operation_mode_store(struct device *dev, @@ -301,7 +303,7 @@ static ssize_t picolcd_operation_mode_de { struct picolcd_data *data = dev_get_drvdata(dev); - return snprintf(buf, PAGE_SIZE, "hello world\n"); + return sysfs_emit(buf, "hello world\n"); } static ssize_t picolcd_operation_mode_delay_store(struct device *dev, CC: Julia Lawall <Julia.Lawall@inria.fr> CC: Nicolas Palix <nicolas.palix@imag.fr> CC: cocci@inria.fr Signed-off-by: Li Zhijian <lizhijian@fujitsu.com>
1 parent 556e2d1 commit 68ea60a

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

scripts/coccinelle/api/device_attr_show.cocci

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
// SPDX-License-Identifier: GPL-2.0-only
22
///
33
/// From Documentation/filesystems/sysfs.rst:
4-
/// show() must not use snprintf() when formatting the value to be
5-
/// returned to user space. If you can guarantee that an overflow
6-
/// will never happen you can use sprintf() otherwise you must use
7-
/// scnprintf().
4+
/// show() should only use sysfs_emit() or sysfs_emit_at() when formatting
5+
/// the value to be returned to user space.
86
///
97
// Confidence: High
108
// Copyright: (C) 2020 Denis Efremov ISPRAS
@@ -30,26 +28,32 @@ ssize_t show(struct device *dev, struct device_attribute *attr, char *buf)
3028

3129
@rp depends on patch@
3230
identifier show, dev, attr, buf;
31+
expression BUF, SZ, FORMAT, STR;
3332
@@
3433

3534
ssize_t show(struct device *dev, struct device_attribute *attr, char *buf)
3635
{
3736
<...
37+
(
3838
return
39-
- snprintf
40-
+ scnprintf
41-
(...);
39+
- snprintf(BUF, SZ, FORMAT, STR);
40+
+ sysfs_emit(BUF, FORMAT, STR);
41+
|
42+
return
43+
- snprintf(BUF, SZ, STR);
44+
+ sysfs_emit(BUF, STR);
45+
)
4246
...>
4347
}
4448

4549
@script: python depends on report@
4650
p << r.p;
4751
@@
4852

49-
coccilib.report.print_report(p[0], "WARNING: use scnprintf or sprintf")
53+
coccilib.report.print_report(p[0], "WARNING: please use sysfs_emit or sysfs_emit_at")
5054

5155
@script: python depends on org@
5256
p << r.p;
5357
@@
5458

55-
coccilib.org.print_todo(p[0], "WARNING: use scnprintf or sprintf")
59+
coccilib.org.print_todo(p[0], "WARNING: please use sysfs_emit or sysfs_emit_at")

0 commit comments

Comments
 (0)