Skip to content

Commit abfc19f

Browse files
evdenisJuliaLawall
authored andcommitted
coccinelle: api: add device_attr_show script
According to the documentation[1] show() methods of device attributes should return the number of bytes printed into the buffer. This is the return value of scnprintf(). show() must not use snprintf() when formatting the value to be returned to user space. snprintf() returns the length the resulting string would be, assuming it all fit into the destination array[2]. scnprintf() return the length of the string actually created in buf. If one can guarantee that an overflow will never happen sprintf() can be used otherwise scnprintf(). [1] Documentation/filesystems/sysfs.txt [2] "snprintf() confusion" https://lwn.net/Articles/69419/ Signed-off-by: Denis Efremov <[email protected]> Signed-off-by: Julia Lawall <[email protected]>
1 parent c0842fb commit abfc19f

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
///
3+
/// From Documentation/filesystems/sysfs.txt:
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().
8+
///
9+
// Confidence: High
10+
// Copyright: (C) 2020 Denis Efremov ISPRAS
11+
// Options: --no-includes --include-headers
12+
//
13+
14+
virtual report
15+
virtual org
16+
virtual context
17+
virtual patch
18+
19+
@r depends on !patch@
20+
identifier show, dev, attr, buf;
21+
position p;
22+
@@
23+
24+
ssize_t show(struct device *dev, struct device_attribute *attr, char *buf)
25+
{
26+
<...
27+
* return snprintf@p(...);
28+
...>
29+
}
30+
31+
@rp depends on patch@
32+
identifier show, dev, attr, buf;
33+
@@
34+
35+
ssize_t show(struct device *dev, struct device_attribute *attr, char *buf)
36+
{
37+
<...
38+
return
39+
- snprintf
40+
+ scnprintf
41+
(...);
42+
...>
43+
}
44+
45+
@script: python depends on report@
46+
p << r.p;
47+
@@
48+
49+
coccilib.report.print_report(p[0], "WARNING: use scnprintf or sprintf")
50+
51+
@script: python depends on org@
52+
p << r.p;
53+
@@
54+
55+
coccilib.org.print_todo(p[0], "WARNING: use scnprintf or sprintf")

0 commit comments

Comments
 (0)