Skip to content

Commit 29a36d4

Browse files
JuliaLawallmichal42
authored andcommitted
scripts/coccinelle: improve the coverage of some semantic patches
This patch ensures that all semantic patches in the scripts/coccinelle directory provide the report option. Report messages that include line numbers now have the line number preceded by "line" for easier subsequent processing. Signed-off-by: Julia Lawall <Julia.Lawall@lip6.fr> Signed-off-by: Michal Marek <mmarek@suse.cz>
1 parent fb3f8af commit 29a36d4

11 files changed

Lines changed: 351 additions & 76 deletions

File tree

‎scripts/coccinelle/api/kstrdup.cocci‎

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
/// Use kstrdup rather than duplicating its implementation
22
///
33
// Confidence: High
4-
// Copyright: (C) 2010 Nicolas Palix, DIKU. GPLv2.
5-
// Copyright: (C) 2010 Julia Lawall, DIKU. GPLv2.
6-
// Copyright: (C) 2010 Gilles Muller, INRIA/LiP6. GPLv2.
4+
// Copyright: (C) 2010-2012 Nicolas Palix. GPLv2.
5+
// Copyright: (C) 2010-2012 Julia Lawall, INRIA/LIP6. GPLv2.
6+
// Copyright: (C) 2010-2012 Gilles Muller, INRIA/LiP6. GPLv2.
77
// URL: http://coccinelle.lip6.fr/
88
// Comments:
99
// Options: -no_includes -include_headers
1010

1111
virtual patch
12+
virtual context
13+
virtual org
14+
virtual report
1215

13-
@@
16+
@depends on patch@
1417
expression from,to;
1518
expression flag,E1,E2;
1619
statement S;
@@ -23,7 +26,7 @@ statement S;
2326
... when != \(from = E2 \| to = E2 \)
2427
- strcpy(to, from);
2528

26-
@@
29+
@depends on patch@
2730
expression x,from,to;
2831
expression flag,E1,E2,E3;
2932
statement S;
@@ -37,3 +40,65 @@ statement S;
3740
if (to==NULL || ...) S
3841
... when != \(x = E3 \| from = E3 \| to = E3 \)
3942
- memcpy(to, from, x);
43+
44+
// ---------------------------------------------------------------------
45+
46+
@r1 depends on !patch exists@
47+
expression from,to;
48+
expression flag,E1,E2;
49+
statement S;
50+
position p1,p2;
51+
@@
52+
53+
* to = kmalloc@p1(strlen(from) + 1,flag);
54+
... when != \(from = E1 \| to = E1 \)
55+
if (to==NULL || ...) S
56+
... when != \(from = E2 \| to = E2 \)
57+
* strcpy@p2(to, from);
58+
59+
@r2 depends on !patch exists@
60+
expression x,from,to;
61+
expression flag,E1,E2,E3;
62+
statement S;
63+
position p1,p2;
64+
@@
65+
66+
* x = strlen(from) + 1;
67+
... when != \( x = E1 \| from = E1 \)
68+
* to = \(kmalloc@p1\|kzalloc@p2\)(x,flag);
69+
... when != \(x = E2 \| from = E2 \| to = E2 \)
70+
if (to==NULL || ...) S
71+
... when != \(x = E3 \| from = E3 \| to = E3 \)
72+
* memcpy@p2(to, from, x);
73+
74+
@script:python depends on org@
75+
p1 << r1.p1;
76+
p2 << r1.p2;
77+
@@
78+
79+
cocci.print_main("WARNING opportunity for kstrdep",p1)
80+
cocci.print_secs("strcpy",p2)
81+
82+
@script:python depends on org@
83+
p1 << r2.p1;
84+
p2 << r2.p2;
85+
@@
86+
87+
cocci.print_main("WARNING opportunity for kstrdep",p1)
88+
cocci.print_secs("memcpy",p2)
89+
90+
@script:python depends on report@
91+
p1 << r1.p1;
92+
p2 << r1.p2;
93+
@@
94+
95+
msg = "WARNING opportunity for kstrdep (strcpy on line %s)" % (p2[0].line)
96+
coccilib.report.print_report(p1[0], msg)
97+
98+
@script:python depends on report@
99+
p1 << r2.p1;
100+
p2 << r2.p2;
101+
@@
102+
103+
msg = "WARNING opportunity for kstrdep (memcpy on line %s)" % (p2[0].line)
104+
coccilib.report.print_report(p1[0], msg)

‎scripts/coccinelle/api/memdup.cocci‎

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
/// Use kmemdup rather than duplicating its implementation
22
///
33
// Confidence: High
4-
// Copyright: (C) 2010 Nicolas Palix, DIKU. GPLv2.
5-
// Copyright: (C) 2010 Julia Lawall, DIKU. GPLv2.
6-
// Copyright: (C) 2010 Gilles Muller, INRIA/LiP6. GPLv2.
4+
// Copyright: (C) 2010-2012 Nicolas Palix. GPLv2.
5+
// Copyright: (C) 2010-2012 Julia Lawall, INRIA/LIP6. GPLv2.
6+
// Copyright: (C) 2010-2012 Gilles Muller, INRIA/LiP6. GPLv2.
77
// URL: http://coccinelle.lip6.fr/
88
// Comments:
99
// Options: -no_includes -include_headers
1010

1111
virtual patch
12+
virtual context
13+
virtual org
14+
virtual report
1215

1316
@r1@
1417
expression from,to;
@@ -28,7 +31,7 @@ position p;
2831
... when != \( x = E1 \| from = E1 \)
2932
to = \(kmalloc@p\|kzalloc@p\)(x,flag);
3033

31-
@@
34+
@depends on patch@
3235
expression from,to,size,flag;
3336
position p != {r1.p,r2.p};
3437
statement S;
@@ -38,3 +41,26 @@ statement S;
3841
+ to = kmemdup(from,size,flag);
3942
if (to==NULL || ...) S
4043
- memcpy(to, from, size);
44+
45+
@r depends on !patch@
46+
expression from,to,size,flag;
47+
position p != {r1.p,r2.p};
48+
statement S;
49+
@@
50+
51+
* to = \(kmalloc@p\|kzalloc@p\)(size,flag);
52+
to = kmemdup(from,size,flag);
53+
if (to==NULL || ...) S
54+
* memcpy(to, from, size);
55+
56+
@script:python depends on org@
57+
p << r.p;
58+
@@
59+
60+
coccilib.org.print_todo(p[0], "WARNING opportunity for kmemdep")
61+
62+
@script:python depends on report@
63+
p << r.p;
64+
@@
65+
66+
coccilib.report.print_report(p[0], "WARNING opportunity for kmemdep")

‎scripts/coccinelle/api/memdup_user.cocci‎

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1-
/// Use kmemdup_user rather than duplicating its implementation
1+
/// Use memdup_user rather than duplicating its implementation
22
/// This is a little bit restricted to reduce false positives
33
///
44
// Confidence: High
5-
// Copyright: (C) 2010 Nicolas Palix, DIKU. GPLv2.
6-
// Copyright: (C) 2010 Julia Lawall, DIKU. GPLv2.
7-
// Copyright: (C) 2010 Gilles Muller, INRIA/LiP6. GPLv2.
5+
// Copyright: (C) 2010-2012 Nicolas Palix. GPLv2.
6+
// Copyright: (C) 2010-2012 Julia Lawall, INRIA/LIP6. GPLv2.
7+
// Copyright: (C) 2010-2012 Gilles Muller, INRIA/LiP6. GPLv2.
88
// URL: http://coccinelle.lip6.fr/
99
// Comments:
1010
// Options: -no_includes -include_headers
1111

1212
virtual patch
13+
virtual context
14+
virtual org
15+
virtual report
1316

14-
@@
17+
@depends on patch@
1518
expression from,to,size,flag;
16-
position p;
1719
identifier l1,l2;
1820
@@
1921

20-
- to = \(kmalloc@p\|kzalloc@p\)(size,flag);
22+
- to = \(kmalloc\|kzalloc\)(size,flag);
2123
+ to = memdup_user(from,size);
2224
if (
2325
- to==NULL
@@ -33,3 +35,26 @@ identifier l1,l2;
3335
- -EFAULT
3436
- ...+>
3537
- }
38+
39+
@r depends on !patch@
40+
expression from,to,size,flag;
41+
position p;
42+
statement S1,S2;
43+
@@
44+
45+
* to = \(kmalloc@p\|kzalloc@p\)(size,flag);
46+
if (to==NULL || ...) S1
47+
if (copy_from_user(to, from, size) != 0)
48+
S2
49+
50+
@script:python depends on org@
51+
p << r.p;
52+
@@
53+
54+
coccilib.org.print_todo(p[0], "WARNING opportunity for memdep_user")
55+
56+
@script:python depends on report@
57+
p << r.p;
58+
@@
59+
60+
coccilib.report.print_report(p[0], "WARNING opportunity for memdep_user")

‎scripts/coccinelle/free/kfree.cocci‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
//# SCTP_DBG_OBJCNT_DEC that do not actually evaluate their argument
66
///
77
// Confidence: Moderate
8-
// Copyright: (C) 2010 Nicolas Palix, DIKU. GPLv2.
9-
// Copyright: (C) 2010 Julia Lawall, DIKU. GPLv2.
10-
// Copyright: (C) 2010 Gilles Muller, INRIA/LiP6. GPLv2.
8+
// Copyright: (C) 2010-2012 Nicolas Palix. GPLv2.
9+
// Copyright: (C) 2010-2012 Julia Lawall, INRIA/LIP6. GPLv2.
10+
// Copyright: (C) 2010-2012 Gilles Muller, INRIA/LiP6. GPLv2.
1111
// URL: http://coccinelle.lip6.fr/
1212
// Comments:
1313
// Options: -no_includes -include_headers
@@ -23,7 +23,7 @@ position p1;
2323
kfree@p1(E)
2424

2525
@print expression@
26-
constant char *c;
26+
constant char [] c;
2727
expression free.E,E2;
2828
type T;
2929
position p;
@@ -36,6 +36,10 @@ identifier f;
3636
E@p == E2
3737
|
3838
E@p != E2
39+
|
40+
E2 == E@p
41+
|
42+
E2 != E@p
3943
|
4044
!E@p
4145
|
@@ -113,5 +117,5 @@ p1 << free.p1;
113117
p2 << r.p2;
114118
@@
115119
116-
msg = "reference preceded by free on line %s" % (p1[0].line)
120+
msg = "ERROR: reference preceded by free on line %s" % (p1[0].line)
117121
coccilib.report.print_report(p2[0],msg)

‎scripts/coccinelle/iterators/fen.cocci‎

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22
/// is no point to call of_node_put on the final value.
33
///
44
// Confidence: High
5-
// Copyright: (C) 2010 Nicolas Palix, DIKU. GPLv2.
6-
// Copyright: (C) 2010 Julia Lawall, DIKU. GPLv2.
7-
// Copyright: (C) 2010 Gilles Muller, INRIA/LiP6. GPLv2.
5+
// Copyright: (C) 2010-2012 Nicolas Palix. GPLv2.
6+
// Copyright: (C) 2010-2012 Julia Lawall, INRIA/LIP6. GPLv2.
7+
// Copyright: (C) 2010-2012 Gilles Muller, INRIA/LiP6. GPLv2.
88
// URL: http://coccinelle.lip6.fr/
99
// Comments:
1010
// Options: -no_includes -include_headers
1111

1212
virtual patch
13+
virtual context
14+
virtual org
15+
virtual report
1316

14-
@@
17+
@depends on patch@
1518
iterator name for_each_node_by_name;
1619
expression np,E;
1720
identifier l;
@@ -24,7 +27,7 @@ for_each_node_by_name(np,...) {
2427
... when != np = E
2528
- of_node_put(np);
2629

27-
@@
30+
@depends on patch@
2831
iterator name for_each_node_by_type;
2932
expression np,E;
3033
identifier l;
@@ -37,7 +40,7 @@ for_each_node_by_type(np,...) {
3740
... when != np = E
3841
- of_node_put(np);
3942

40-
@@
43+
@depends on patch@
4144
iterator name for_each_compatible_node;
4245
expression np,E;
4346
identifier l;
@@ -50,7 +53,7 @@ for_each_compatible_node(np,...) {
5053
... when != np = E
5154
- of_node_put(np);
5255

53-
@@
56+
@depends on patch@
5457
iterator name for_each_matching_node;
5558
expression np,E;
5659
identifier l;
@@ -62,3 +65,59 @@ for_each_matching_node(np,...) {
6265
}
6366
... when != np = E
6467
- of_node_put(np);
68+
69+
// ----------------------------------------------------------------------
70+
71+
@r depends on !patch forall@
72+
//iterator name for_each_node_by_name;
73+
//iterator name for_each_node_by_type;
74+
//iterator name for_each_compatible_node;
75+
//iterator name for_each_matching_node;
76+
expression np,E;
77+
identifier l;
78+
position p1,p2;
79+
@@
80+
81+
(
82+
*for_each_node_by_name@p1(np,...)
83+
{
84+
... when != break;
85+
when != goto l;
86+
}
87+
|
88+
*for_each_node_by_type@p1(np,...)
89+
{
90+
... when != break;
91+
when != goto l;
92+
}
93+
|
94+
*for_each_compatible_node@p1(np,...)
95+
{
96+
... when != break;
97+
when != goto l;
98+
}
99+
|
100+
*for_each_matching_node@p1(np,...)
101+
{
102+
... when != break;
103+
when != goto l;
104+
}
105+
)
106+
... when != np = E
107+
* of_node_put@p2(np);
108+
109+
@script:python depends on org@
110+
p1 << r.p1;
111+
p2 << r.p2;
112+
@@
113+
114+
cocci.print_main("unneeded of_node_put",p2)
115+
cocci.print_secs("iterator",p1)
116+
117+
@script:python depends on report@
118+
p1 << r.p1;
119+
p2 << r.p2;
120+
@@
121+
122+
msg = "ERROR: of_node_put not needed after iterator on line %s" % (p1[0].line)
123+
coccilib.report.print_report(p2[0], msg)

0 commit comments

Comments
 (0)