Commit 4320e2fda40ae5057777d4700192a0c55287b66a
Committed by
Tom Rini
1 parent
428e60e079
Exists in
smarc_8mq_lf_v2020.04
and in
17 other branches
scripts/coccinelle: add some more coccinelle tests
kmerr: verify that malloc and calloc are followed by a check to verify that we are not out of memory. badzero: Compare pointer-typed values to NULL rather than 0 Both checks are copied from the Linux kernel archive. Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Showing 2 changed files with 316 additions and 0 deletions Side-by-side Diff
scripts/coccinelle/null/badzero.cocci
1 | +/// Compare pointer-typed values to NULL rather than 0 | |
2 | +/// | |
3 | +//# This makes an effort to choose between !x and x == NULL. !x is used | |
4 | +//# if it has previously been used with the function used to initialize x. | |
5 | +//# This relies on type information. More type information can be obtained | |
6 | +//# using the option -all_includes and the option -I to specify an | |
7 | +//# include path. | |
8 | +// | |
9 | +// Confidence: High | |
10 | +// Copyright: (C) 2012 Julia Lawall, INRIA/LIP6. GPLv2. | |
11 | +// Copyright: (C) 2012 Gilles Muller, INRIA/LiP6. GPLv2. | |
12 | +// URL: http://coccinelle.lip6.fr/ | |
13 | +// Requires: 1.0.0 | |
14 | +// Options: | |
15 | +// | |
16 | +// SPDX-License-Identifier: GPL-2.0 | |
17 | +// | |
18 | + | |
19 | +virtual patch | |
20 | +virtual context | |
21 | +virtual org | |
22 | +virtual report | |
23 | + | |
24 | +@initialize:ocaml@ | |
25 | +@@ | |
26 | +let negtable = Hashtbl.create 101 | |
27 | + | |
28 | +@depends on patch@ | |
29 | +expression *E; | |
30 | +identifier f; | |
31 | +@@ | |
32 | + | |
33 | +( | |
34 | + (E = f(...)) == | |
35 | +- 0 | |
36 | ++ NULL | |
37 | +| | |
38 | + (E = f(...)) != | |
39 | +- 0 | |
40 | ++ NULL | |
41 | +| | |
42 | +- 0 | |
43 | ++ NULL | |
44 | + == (E = f(...)) | |
45 | +| | |
46 | +- 0 | |
47 | ++ NULL | |
48 | + != (E = f(...)) | |
49 | +) | |
50 | + | |
51 | + | |
52 | +@t1 depends on !patch@ | |
53 | +expression *E; | |
54 | +identifier f; | |
55 | +position p; | |
56 | +@@ | |
57 | + | |
58 | +( | |
59 | + (E = f(...)) == | |
60 | +* 0@p | |
61 | +| | |
62 | + (E = f(...)) != | |
63 | +* 0@p | |
64 | +| | |
65 | +* 0@p | |
66 | + == (E = f(...)) | |
67 | +| | |
68 | +* 0@p | |
69 | + != (E = f(...)) | |
70 | +) | |
71 | + | |
72 | +@script:python depends on org@ | |
73 | +p << t1.p; | |
74 | +@@ | |
75 | + | |
76 | +coccilib.org.print_todo(p[0], "WARNING comparing pointer to 0") | |
77 | + | |
78 | +@script:python depends on report@ | |
79 | +p << t1.p; | |
80 | +@@ | |
81 | + | |
82 | +coccilib.report.print_report(p[0], "WARNING comparing pointer to 0") | |
83 | + | |
84 | +// Tests of returned values | |
85 | + | |
86 | +@s@ | |
87 | +identifier f; | |
88 | +expression E,E1; | |
89 | +@@ | |
90 | + | |
91 | + E = f(...) | |
92 | + ... when != E = E1 | |
93 | + !E | |
94 | + | |
95 | +@script:ocaml depends on s@ | |
96 | +f << s.f; | |
97 | +@@ | |
98 | + | |
99 | +try let _ = Hashtbl.find negtable f in () | |
100 | +with Not_found -> Hashtbl.add negtable f () | |
101 | + | |
102 | +@ r disable is_zero,isnt_zero exists @ | |
103 | +expression *E; | |
104 | +identifier f; | |
105 | +@@ | |
106 | + | |
107 | +E = f(...) | |
108 | +... | |
109 | +(E == 0 | |
110 | +|E != 0 | |
111 | +|0 == E | |
112 | +|0 != E | |
113 | +) | |
114 | + | |
115 | +@script:ocaml@ | |
116 | +f << r.f; | |
117 | +@@ | |
118 | + | |
119 | +try let _ = Hashtbl.find negtable f in () | |
120 | +with Not_found -> include_match false | |
121 | + | |
122 | +// This rule may lead to inconsistent path problems, if E is defined in two | |
123 | +// places | |
124 | +@ depends on patch disable is_zero,isnt_zero @ | |
125 | +expression *E; | |
126 | +expression E1; | |
127 | +identifier r.f; | |
128 | +@@ | |
129 | + | |
130 | +E = f(...) | |
131 | +<... | |
132 | +( | |
133 | +- E == 0 | |
134 | ++ !E | |
135 | +| | |
136 | +- E != 0 | |
137 | ++ E | |
138 | +| | |
139 | +- 0 == E | |
140 | ++ !E | |
141 | +| | |
142 | +- 0 != E | |
143 | ++ E | |
144 | +) | |
145 | +...> | |
146 | +?E = E1 | |
147 | + | |
148 | +@t2 depends on !patch disable is_zero,isnt_zero @ | |
149 | +expression *E; | |
150 | +expression E1; | |
151 | +identifier r.f; | |
152 | +position p1; | |
153 | +position p2; | |
154 | +@@ | |
155 | + | |
156 | +E = f(...) | |
157 | +<... | |
158 | +( | |
159 | +* E == 0@p1 | |
160 | +| | |
161 | +* E != 0@p2 | |
162 | +| | |
163 | +* 0@p1 == E | |
164 | +| | |
165 | +* 0@p1 != E | |
166 | +) | |
167 | +...> | |
168 | +?E = E1 | |
169 | + | |
170 | +@script:python depends on org@ | |
171 | +p << t2.p1; | |
172 | +@@ | |
173 | + | |
174 | +coccilib.org.print_todo(p[0], "WARNING comparing pointer to 0, suggest !E") | |
175 | + | |
176 | +@script:python depends on org@ | |
177 | +p << t2.p2; | |
178 | +@@ | |
179 | + | |
180 | +coccilib.org.print_todo(p[0], "WARNING comparing pointer to 0") | |
181 | + | |
182 | +@script:python depends on report@ | |
183 | +p << t2.p1; | |
184 | +@@ | |
185 | + | |
186 | +coccilib.report.print_report(p[0], "WARNING comparing pointer to 0, suggest !E") | |
187 | + | |
188 | +@script:python depends on report@ | |
189 | +p << t2.p2; | |
190 | +@@ | |
191 | + | |
192 | +coccilib.report.print_report(p[0], "WARNING comparing pointer to 0") | |
193 | + | |
194 | +@ depends on patch disable is_zero,isnt_zero @ | |
195 | +expression *E; | |
196 | +@@ | |
197 | + | |
198 | +( | |
199 | + E == | |
200 | +- 0 | |
201 | ++ NULL | |
202 | +| | |
203 | + E != | |
204 | +- 0 | |
205 | ++ NULL | |
206 | +| | |
207 | +- 0 | |
208 | ++ NULL | |
209 | + == E | |
210 | +| | |
211 | +- 0 | |
212 | ++ NULL | |
213 | + != E | |
214 | +) | |
215 | + | |
216 | +@ t3 depends on !patch disable is_zero,isnt_zero @ | |
217 | +expression *E; | |
218 | +position p; | |
219 | +@@ | |
220 | + | |
221 | +( | |
222 | +* E == 0@p | |
223 | +| | |
224 | +* E != 0@p | |
225 | +| | |
226 | +* 0@p == E | |
227 | +| | |
228 | +* 0@p != E | |
229 | +) | |
230 | + | |
231 | +@script:python depends on org@ | |
232 | +p << t3.p; | |
233 | +@@ | |
234 | + | |
235 | +coccilib.org.print_todo(p[0], "WARNING comparing pointer to 0") | |
236 | + | |
237 | +@script:python depends on report@ | |
238 | +p << t3.p; | |
239 | +@@ | |
240 | + | |
241 | +coccilib.report.print_report(p[0], "WARNING comparing pointer to 0") |
scripts/coccinelle/null/kmerr.cocci
1 | +/// This semantic patch looks for malloc etc that are not followed by a | |
2 | +/// NULL check. It only gives a report in the case where there is some | |
3 | +/// error handling code later in the function, which may be helpful | |
4 | +/// in determining what the error handling code for the call to malloc etc | |
5 | +/// should be. | |
6 | +/// | |
7 | +// Confidence: High | |
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. | |
11 | +// URL: http://coccinelle.lip6.fr/ | |
12 | +// Comments: | |
13 | +// Options: --no-includes --include-headers | |
14 | +// | |
15 | +// SPDX-License-Identifier: GPL-2.0 | |
16 | +// | |
17 | + | |
18 | +virtual context | |
19 | +virtual org | |
20 | +virtual report | |
21 | + | |
22 | +@withtest@ | |
23 | +expression x; | |
24 | +position p; | |
25 | +identifier f,fld; | |
26 | +@@ | |
27 | + | |
28 | +x@p = f(...); | |
29 | +... when != x->fld | |
30 | +\(x == NULL \| x != NULL\) | |
31 | + | |
32 | +@fixed depends on context && !org && !report@ | |
33 | +expression x,x1; | |
34 | +position p1 != withtest.p; | |
35 | +statement S; | |
36 | +position any withtest.p; | |
37 | +identifier f; | |
38 | +@@ | |
39 | + | |
40 | +*x@p1 = \(malloc\|calloc\)(...); | |
41 | +... | |
42 | +*x1@p = f(...); | |
43 | +if (!x1) S | |
44 | + | |
45 | +// ------------------------------------------------------------------------ | |
46 | + | |
47 | +@rfixed depends on (org || report) && !context exists@ | |
48 | +expression x,x1; | |
49 | +position p1 != withtest.p; | |
50 | +position p2; | |
51 | +statement S; | |
52 | +position any withtest.p; | |
53 | +identifier f; | |
54 | +@@ | |
55 | + | |
56 | +x@p1 = \(malloc\|calloc\)(...); | |
57 | +... | |
58 | +x1@p = f@p2(...); | |
59 | +if (!x1) S | |
60 | + | |
61 | +@script:python depends on org@ | |
62 | +p1 << rfixed.p1; | |
63 | +p2 << rfixed.p2; | |
64 | +@@ | |
65 | + | |
66 | +cocci.print_main("alloc call",p1) | |
67 | +cocci.print_secs("possible model",p2) | |
68 | + | |
69 | +@script:python depends on report@ | |
70 | +p1 << rfixed.p1; | |
71 | +p2 << rfixed.p2; | |
72 | +@@ | |
73 | + | |
74 | +msg = "alloc with no test, possible model on line %s" % (p2[0].line) | |
75 | +coccilib.report.print_report(p1[0],msg) |