GNU Linux-libre 4.19.245-gnu1
[releases.git] / scripts / coccinelle / api / alloc / zalloc-simple.cocci
1 ///
2 /// Use zeroing allocator rather than allocator followed by memset with 0
3 ///
4 /// This considers some simple cases that are common and easy to validate
5 /// Note in particular that there are no ...s in the rule, so all of the
6 /// matched code has to be contiguous
7 ///
8 // Confidence: High
9 // Copyright: (C) 2009-2010 Julia Lawall, Nicolas Palix, DIKU.  GPLv2.
10 // Copyright: (C) 2009-2010 Gilles Muller, INRIA/LiP6.  GPLv2.
11 // Copyright: (C) 2017 Himanshu Jha GPLv2.
12 // URL: http://coccinelle.lip6.fr/rules/kzalloc.html
13 // Options: --no-includes --include-headers
14 //
15 // Keywords: kmalloc, kzalloc
16 // Version min: < 2.6.12 kmalloc
17 // Version min:   2.6.14 kzalloc
18 //
19
20 virtual context
21 virtual patch
22 virtual org
23 virtual report
24
25 //----------------------------------------------------------
26 //  For context mode
27 //----------------------------------------------------------
28
29 @depends on context@
30 type T, T2;
31 expression x;
32 expression E1;
33 statement S;
34 @@
35
36 * x = (T)\(kmalloc(E1, ...)\|vmalloc(E1)\|dma_alloc_coherent(...,E1,...)\|
37   kmalloc_node(E1, ...)\|kmem_cache_alloc(...)\|kmem_alloc(E1, ...)\|
38   devm_kmalloc(...,E1,...)\|kvmalloc(E1, ...)\|kvmalloc_node(E1,...)\);
39   if ((x==NULL) || ...) S
40 * memset((T2)x,0,E1);
41
42 //----------------------------------------------------------
43 //  For patch mode
44 //----------------------------------------------------------
45
46 @depends on patch@
47 type T, T2;
48 expression x;
49 expression E1,E2,E3,E4;
50 statement S;
51 @@
52
53 (
54 - x = kmalloc(E1,E2);
55 + x = kzalloc(E1,E2);
56 |
57 - x = (T *)kmalloc(E1,E2);
58 + x = kzalloc(E1,E2);
59 |
60 - x = (T)kmalloc(E1,E2);
61 + x = (T)kzalloc(E1,E2);
62 |
63 - x = vmalloc(E1);
64 + x = vzalloc(E1);
65 |
66 - x = (T *)vmalloc(E1);
67 + x = vzalloc(E1);
68 |
69 - x = (T)vmalloc(E1);
70 + x = (T)vzalloc(E1);
71 |
72 - x = dma_alloc_coherent(E2,E1,E3,E4);
73 + x = dma_zalloc_coherent(E2,E1,E3,E4);
74 |
75 - x = (T *)dma_alloc_coherent(E2,E1,E3,E4);
76 + x = dma_zalloc_coherent(E2,E1,E3,E4);
77 |
78 - x = (T)dma_alloc_coherent(E2,E1,E3,E4);
79 + x = (T)dma_zalloc_coherent(E2,E1,E3,E4);
80 |
81 - x = kmalloc_node(E1,E2,E3);
82 + x = kzalloc_node(E1,E2,E3);
83 |
84 - x = (T *)kmalloc_node(E1,E2,E3);
85 + x = kzalloc_node(E1,E2,E3);
86 |
87 - x = (T)kmalloc_node(E1,E2,E3);
88 + x = (T)kzalloc_node(E1,E2,E3);
89 |
90 - x = kmem_cache_alloc(E3,E4);
91 + x = kmem_cache_zalloc(E3,E4);
92 |
93 - x = (T *)kmem_cache_alloc(E3,E4);
94 + x = kmem_cache_zalloc(E3,E4);
95 |
96 - x = (T)kmem_cache_alloc(E3,E4);
97 + x = (T)kmem_cache_zalloc(E3,E4);
98 |
99 - x = kmem_alloc(E1,E2);
100 + x = kmem_zalloc(E1,E2);
101 |
102 - x = (T *)kmem_alloc(E1,E2);
103 + x = kmem_zalloc(E1,E2);
104 |
105 - x = (T)kmem_alloc(E1,E2);
106 + x = (T)kmem_zalloc(E1,E2);
107 |
108 - x = devm_kmalloc(E2,E1,E3);
109 + x = devm_kzalloc(E2,E1,E3);
110 |
111 - x = (T *)devm_kmalloc(E2,E1,E3);
112 + x = devm_kzalloc(E2,E1,E3);
113 |
114 - x = (T)devm_kmalloc(E2,E1,E3);
115 + x = (T)devm_kzalloc(E2,E1,E3);
116 |
117 - x = kvmalloc(E1,E2);
118 + x = kvzalloc(E1,E2);
119 |
120 - x = (T *)kvmalloc(E1,E2);
121 + x = kvzalloc(E1,E2);
122 |
123 - x = (T)kvmalloc(E1,E2);
124 + x = (T)kvzalloc(E1,E2);
125 |
126 - x = kvmalloc_node(E1,E2,E3);
127 + x = kvzalloc_node(E1,E2,E3);
128 |
129 - x = (T *)kvmalloc_node(E1,E2,E3);
130 + x = kvzalloc_node(E1,E2,E3);
131 |
132 - x = (T)kvmalloc_node(E1,E2,E3);
133 + x = (T)kvzalloc_node(E1,E2,E3);
134 )
135   if ((x==NULL) || ...) S
136 - memset((T2)x,0,E1);
137
138 //----------------------------------------------------------
139 //  For org mode
140 //----------------------------------------------------------
141
142 @r depends on org || report@
143 type T, T2;
144 expression x;
145 expression E1,E2;
146 statement S;
147 position p;
148 @@
149
150  x = (T)kmalloc@p(E1,E2);
151  if ((x==NULL) || ...) S
152  memset((T2)x,0,E1);
153
154 @script:python depends on org@
155 p << r.p;
156 x << r.x;
157 @@
158
159 msg="%s" % (x)
160 msg_safe=msg.replace("[","@(").replace("]",")")
161 coccilib.org.print_todo(p[0], msg_safe)
162
163 @script:python depends on report@
164 p << r.p;
165 x << r.x;
166 @@
167
168 msg="WARNING: kzalloc should be used for %s, instead of kmalloc/memset" % (x)
169 coccilib.report.print_report(p[0], msg)
170
171 //-----------------------------------------------------------------
172 @r1 depends on org || report@
173 type T, T2;
174 expression x;
175 expression E1;
176 statement S;
177 position p;
178 @@
179
180  x = (T)vmalloc@p(E1);
181  if ((x==NULL) || ...) S
182  memset((T2)x,0,E1);
183
184 @script:python depends on org@
185 p << r1.p;
186 x << r1.x;
187 @@
188
189 msg="%s" % (x)
190 msg_safe=msg.replace("[","@(").replace("]",")")
191 coccilib.org.print_todo(p[0], msg_safe)
192
193 @script:python depends on report@
194 p << r1.p;
195 x << r1.x;
196 @@
197
198 msg="WARNING: vzalloc should be used for %s, instead of vmalloc/memset" % (x)
199 coccilib.report.print_report(p[0], msg)
200
201 //-----------------------------------------------------------------
202 @r2 depends on org || report@
203 type T, T2;
204 expression x;
205 expression E1,E2,E3,E4;
206 statement S;
207 position p;
208 @@
209
210  x = (T)dma_alloc_coherent@p(E2,E1,E3,E4);
211  if ((x==NULL) || ...) S
212  memset((T2)x,0,E1);
213
214 @script:python depends on org@
215 p << r2.p;
216 x << r2.x;
217 @@
218
219 msg="%s" % (x)
220 msg_safe=msg.replace("[","@(").replace("]",")")
221 coccilib.org.print_todo(p[0], msg_safe)
222
223 @script:python depends on report@
224 p << r2.p;
225 x << r2.x;
226 @@
227
228 msg="WARNING: dma_zalloc_coherent should be used for %s, instead of dma_alloc_coherent/memset" % (x)
229 coccilib.report.print_report(p[0], msg)
230
231 //-----------------------------------------------------------------
232 @r3 depends on org || report@
233 type T, T2;
234 expression x;
235 expression E1,E2,E3;
236 statement S;
237 position p;
238 @@
239
240  x = (T)kmalloc_node@p(E1,E2,E3);
241  if ((x==NULL) || ...) S
242  memset((T2)x,0,E1);
243
244 @script:python depends on org@
245 p << r3.p;
246 x << r3.x;
247 @@
248
249 msg="%s" % (x)
250 msg_safe=msg.replace("[","@(").replace("]",")")
251 coccilib.org.print_todo(p[0], msg_safe)
252
253 @script:python depends on report@
254 p << r3.p;
255 x << r3.x;
256 @@
257
258 msg="WARNING: kzalloc_node should be used for %s, instead of kmalloc_node/memset" % (x)
259 coccilib.report.print_report(p[0], msg)
260
261 //-----------------------------------------------------------------
262 @r4 depends on org || report@
263 type T, T2;
264 expression x;
265 expression E1,E2,E3;
266 statement S;
267 position p;
268 @@
269
270  x = (T)kmem_cache_alloc@p(E2,E3);
271  if ((x==NULL) || ...) S
272  memset((T2)x,0,E1);
273
274 @script:python depends on org@
275 p << r4.p;
276 x << r4.x;
277 @@
278
279 msg="%s" % (x)
280 msg_safe=msg.replace("[","@(").replace("]",")")
281 coccilib.org.print_todo(p[0], msg_safe)
282
283 @script:python depends on report@
284 p << r4.p;
285 x << r4.x;
286 @@
287
288 msg="WARNING: kmem_cache_zalloc should be used for %s, instead of kmem_cache_alloc/memset" % (x)
289 coccilib.report.print_report(p[0], msg)
290
291 //-----------------------------------------------------------------
292 @r5 depends on org || report@
293 type T, T2;
294 expression x;
295 expression E1,E2;
296 statement S;
297 position p;
298 @@
299
300  x = (T)kmem_alloc@p(E1,E2);
301  if ((x==NULL) || ...) S
302  memset((T2)x,0,E1);
303
304 @script:python depends on org@
305 p << r5.p;
306 x << r5.x;
307 @@
308
309 msg="%s" % (x)
310 msg_safe=msg.replace("[","@(").replace("]",")")
311 coccilib.org.print_todo(p[0], msg_safe)
312
313 @script:python depends on report@
314 p << r5.p;
315 x << r5.x;
316 @@
317
318 msg="WARNING: kmem_zalloc should be used for %s, instead of kmem_alloc/memset" % (x)
319 coccilib.report.print_report(p[0], msg)
320
321 //-----------------------------------------------------------------
322 @r6 depends on org || report@
323 type T, T2;
324 expression x;
325 expression E1,E2,E3;
326 statement S;
327 position p;
328 @@
329
330  x = (T)devm_kmalloc@p(E2,E1,E3);
331  if ((x==NULL) || ...) S
332  memset((T2)x,0,E1);
333
334 @script:python depends on org@
335 p << r6.p;
336 x << r6.x;
337 @@
338
339 msg="%s" % (x)
340 msg_safe=msg.replace("[","@(").replace("]",")")
341 coccilib.org.print_todo(p[0], msg_safe)
342
343 @script:python depends on report@
344 p << r6.p;
345 x << r6.x;
346 @@
347
348 msg="WARNING: devm_kzalloc should be used for %s, instead of devm_kmalloc/memset" % (x)
349 coccilib.report.print_report(p[0], msg)
350
351 //-----------------------------------------------------------------
352 @r7 depends on org || report@
353 type T, T2;
354 expression x;
355 expression E1,E2;
356 statement S;
357 position p;
358 @@
359
360  x = (T)kvmalloc@p(E1,E2);
361  if ((x==NULL) || ...) S
362  memset((T2)x,0,E1);
363
364 @script:python depends on org@
365 p << r7.p;
366 x << r7.x;
367 @@
368
369 msg="%s" % (x)
370 msg_safe=msg.replace("[","@(").replace("]",")")
371 coccilib.org.print_todo(p[0], msg_safe)
372
373 @script:python depends on report@
374 p << r7.p;
375 x << r7.x;
376 @@
377
378 msg="WARNING: kvzalloc should be used for %s, instead of kvmalloc/memset" % (x)
379 coccilib.report.print_report(p[0], msg)
380
381 //-----------------------------------------------------------------
382 @r9 depends on org || report@
383 type T, T2;
384 expression x;
385 expression E1,E2,E3;
386 statement S;
387 position p;
388 @@
389
390  x = (T)kvmalloc_node@p(E1,E2,E3);
391  if ((x==NULL) || ...) S
392  memset((T2)x,0,E1);
393
394 @script:python depends on org@
395 p << r9.p;
396 x << r9.x;
397 @@
398
399 msg="%s" % (x)
400 msg_safe=msg.replace("[","@(").replace("]",")")
401 coccilib.org.print_todo(p[0], msg_safe)
402
403 @script:python depends on report@
404 p << r9.p;
405 x << r9.x;
406 @@
407
408 msg="WARNING: kvzalloc_node should be used for %s, instead of kvmalloc_node/memset" % (x)
409 coccilib.report.print_report(p[0], msg)