2 /// Use zeroing allocator rather than allocator followed by memset with 0
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
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
15 // Keywords: kmalloc, kzalloc
16 // Version min: < 2.6.12 kmalloc
17 // Version min: 2.6.14 kzalloc
25 //----------------------------------------------------------
27 //----------------------------------------------------------
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, ...)\|pci_alloc_consistent(...,E1,...)\|
39 kvmalloc_node(E1,...)\);
40 if ((x==NULL) || ...) S
43 //----------------------------------------------------------
45 //----------------------------------------------------------
50 expression E1,E2,E3,E4;
58 - x = (T *)kmalloc(E1,E2);
61 - x = (T)kmalloc(E1,E2);
62 + x = (T)kzalloc(E1,E2);
67 - x = (T *)vmalloc(E1);
73 - x = dma_alloc_coherent(E2,E1,E3,E4);
74 + x = dma_zalloc_coherent(E2,E1,E3,E4);
76 - x = (T *)dma_alloc_coherent(E2,E1,E3,E4);
77 + x = dma_zalloc_coherent(E2,E1,E3,E4);
79 - x = (T)dma_alloc_coherent(E2,E1,E3,E4);
80 + x = (T)dma_zalloc_coherent(E2,E1,E3,E4);
82 - x = kmalloc_node(E1,E2,E3);
83 + x = kzalloc_node(E1,E2,E3);
85 - x = (T *)kmalloc_node(E1,E2,E3);
86 + x = kzalloc_node(E1,E2,E3);
88 - x = (T)kmalloc_node(E1,E2,E3);
89 + x = (T)kzalloc_node(E1,E2,E3);
91 - x = kmem_cache_alloc(E3,E4);
92 + x = kmem_cache_zalloc(E3,E4);
94 - x = (T *)kmem_cache_alloc(E3,E4);
95 + x = kmem_cache_zalloc(E3,E4);
97 - x = (T)kmem_cache_alloc(E3,E4);
98 + x = (T)kmem_cache_zalloc(E3,E4);
100 - x = kmem_alloc(E1,E2);
101 + x = kmem_zalloc(E1,E2);
103 - x = (T *)kmem_alloc(E1,E2);
104 + x = kmem_zalloc(E1,E2);
106 - x = (T)kmem_alloc(E1,E2);
107 + x = (T)kmem_zalloc(E1,E2);
109 - x = devm_kmalloc(E2,E1,E3);
110 + x = devm_kzalloc(E2,E1,E3);
112 - x = (T *)devm_kmalloc(E2,E1,E3);
113 + x = devm_kzalloc(E2,E1,E3);
115 - x = (T)devm_kmalloc(E2,E1,E3);
116 + x = (T)devm_kzalloc(E2,E1,E3);
118 - x = kvmalloc(E1,E2);
119 + x = kvzalloc(E1,E2);
121 - x = (T *)kvmalloc(E1,E2);
122 + x = kvzalloc(E1,E2);
124 - x = (T)kvmalloc(E1,E2);
125 + x = (T)kvzalloc(E1,E2);
127 - x = pci_alloc_consistent(E2,E1,E3);
128 + x = pci_zalloc_consistent(E2,E1,E3);
130 - x = (T *)pci_alloc_consistent(E2,E1,E3);
131 + x = pci_zalloc_consistent(E2,E1,E3);
133 - x = (T)pci_alloc_consistent(E2,E1,E3);
134 + x = (T)pci_zalloc_consistent(E2,E1,E3);
136 - x = kvmalloc_node(E1,E2,E3);
137 + x = kvzalloc_node(E1,E2,E3);
139 - x = (T *)kvmalloc_node(E1,E2,E3);
140 + x = kvzalloc_node(E1,E2,E3);
142 - x = (T)kvmalloc_node(E1,E2,E3);
143 + x = (T)kvzalloc_node(E1,E2,E3);
145 if ((x==NULL) || ...) S
146 - memset((T2)x,0,E1);
148 //----------------------------------------------------------
150 //----------------------------------------------------------
152 @r depends on org || report@
160 x = (T)kmalloc@p(E1,E2);
161 if ((x==NULL) || ...) S
164 @script:python depends on org@
170 msg_safe=msg.replace("[","@(").replace("]",")")
171 coccilib.org.print_todo(p[0], msg_safe)
173 @script:python depends on report@
178 msg="WARNING: kzalloc should be used for %s, instead of kmalloc/memset" % (x)
179 coccilib.report.print_report(p[0], msg)
181 //-----------------------------------------------------------------
182 @r1 depends on org || report@
190 x = (T)vmalloc@p(E1);
191 if ((x==NULL) || ...) S
194 @script:python depends on org@
200 msg_safe=msg.replace("[","@(").replace("]",")")
201 coccilib.org.print_todo(p[0], msg_safe)
203 @script:python depends on report@
208 msg="WARNING: vzalloc should be used for %s, instead of vmalloc/memset" % (x)
209 coccilib.report.print_report(p[0], msg)
211 //-----------------------------------------------------------------
212 @r2 depends on org || report@
215 expression E1,E2,E3,E4;
220 x = (T)dma_alloc_coherent@p(E2,E1,E3,E4);
221 if ((x==NULL) || ...) S
224 @script:python depends on org@
230 msg_safe=msg.replace("[","@(").replace("]",")")
231 coccilib.org.print_todo(p[0], msg_safe)
233 @script:python depends on report@
238 msg="WARNING: dma_zalloc_coherent should be used for %s, instead of dma_alloc_coherent/memset" % (x)
239 coccilib.report.print_report(p[0], msg)
241 //-----------------------------------------------------------------
242 @r3 depends on org || report@
250 x = (T)kmalloc_node@p(E1,E2,E3);
251 if ((x==NULL) || ...) S
254 @script:python depends on org@
260 msg_safe=msg.replace("[","@(").replace("]",")")
261 coccilib.org.print_todo(p[0], msg_safe)
263 @script:python depends on report@
268 msg="WARNING: kzalloc_node should be used for %s, instead of kmalloc_node/memset" % (x)
269 coccilib.report.print_report(p[0], msg)
271 //-----------------------------------------------------------------
272 @r4 depends on org || report@
280 x = (T)kmem_cache_alloc@p(E2,E3);
281 if ((x==NULL) || ...) S
284 @script:python depends on org@
290 msg_safe=msg.replace("[","@(").replace("]",")")
291 coccilib.org.print_todo(p[0], msg_safe)
293 @script:python depends on report@
298 msg="WARNING: kmem_cache_zalloc should be used for %s, instead of kmem_cache_alloc/memset" % (x)
299 coccilib.report.print_report(p[0], msg)
301 //-----------------------------------------------------------------
302 @r5 depends on org || report@
310 x = (T)kmem_alloc@p(E1,E2);
311 if ((x==NULL) || ...) S
314 @script:python depends on org@
320 msg_safe=msg.replace("[","@(").replace("]",")")
321 coccilib.org.print_todo(p[0], msg_safe)
323 @script:python depends on report@
328 msg="WARNING: kmem_zalloc should be used for %s, instead of kmem_alloc/memset" % (x)
329 coccilib.report.print_report(p[0], msg)
331 //-----------------------------------------------------------------
332 @r6 depends on org || report@
340 x = (T)devm_kmalloc@p(E2,E1,E3);
341 if ((x==NULL) || ...) S
344 @script:python depends on org@
350 msg_safe=msg.replace("[","@(").replace("]",")")
351 coccilib.org.print_todo(p[0], msg_safe)
353 @script:python depends on report@
358 msg="WARNING: devm_kzalloc should be used for %s, instead of devm_kmalloc/memset" % (x)
359 coccilib.report.print_report(p[0], msg)
361 //-----------------------------------------------------------------
362 @r7 depends on org || report@
370 x = (T)kvmalloc@p(E1,E2);
371 if ((x==NULL) || ...) S
374 @script:python depends on org@
380 msg_safe=msg.replace("[","@(").replace("]",")")
381 coccilib.org.print_todo(p[0], msg_safe)
383 @script:python depends on report@
388 msg="WARNING: kvzalloc should be used for %s, instead of kvmalloc/memset" % (x)
389 coccilib.report.print_report(p[0], msg)
391 //-----------------------------------------------------------------
392 @r8 depends on org || report@
400 x = (T)pci_alloc_consistent@p(E2,E1,E3);
401 if ((x==NULL) || ...) S
404 @script:python depends on org@
410 msg_safe=msg.replace("[","@(").replace("]",")")
411 coccilib.org.print_todo(p[0], msg_safe)
413 @script:python depends on report@
418 msg="WARNING: pci_zalloc_consistent should be used for %s, instead of pci_alloc_consistent/memset" % (x)
419 coccilib.report.print_report(p[0], msg)
420 //-----------------------------------------------------------------
421 @r9 depends on org || report@
429 x = (T)kvmalloc_node@p(E1,E2,E3);
430 if ((x==NULL) || ...) S
433 @script:python depends on org@
439 msg_safe=msg.replace("[","@(").replace("]",")")
440 coccilib.org.print_todo(p[0], msg_safe)
442 @script:python depends on report@
447 msg="WARNING: kvzalloc_node should be used for %s, instead of kvmalloc_node/memset" % (x)
448 coccilib.report.print_report(p[0], msg)