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, ...)\|kvmalloc_node(E1,...)\);
39 if ((x==NULL) || ...) S
42 //----------------------------------------------------------
44 //----------------------------------------------------------
49 expression E1,E2,E3,E4;
57 - x = (T *)kmalloc(E1,E2);
60 - x = (T)kmalloc(E1,E2);
61 + x = (T)kzalloc(E1,E2);
66 - x = (T *)vmalloc(E1);
72 - x = dma_alloc_coherent(E2,E1,E3,E4);
73 + x = dma_zalloc_coherent(E2,E1,E3,E4);
75 - x = (T *)dma_alloc_coherent(E2,E1,E3,E4);
76 + x = dma_zalloc_coherent(E2,E1,E3,E4);
78 - x = (T)dma_alloc_coherent(E2,E1,E3,E4);
79 + x = (T)dma_zalloc_coherent(E2,E1,E3,E4);
81 - x = kmalloc_node(E1,E2,E3);
82 + x = kzalloc_node(E1,E2,E3);
84 - x = (T *)kmalloc_node(E1,E2,E3);
85 + x = kzalloc_node(E1,E2,E3);
87 - x = (T)kmalloc_node(E1,E2,E3);
88 + x = (T)kzalloc_node(E1,E2,E3);
90 - x = kmem_cache_alloc(E3,E4);
91 + x = kmem_cache_zalloc(E3,E4);
93 - x = (T *)kmem_cache_alloc(E3,E4);
94 + x = kmem_cache_zalloc(E3,E4);
96 - x = (T)kmem_cache_alloc(E3,E4);
97 + x = (T)kmem_cache_zalloc(E3,E4);
99 - x = kmem_alloc(E1,E2);
100 + x = kmem_zalloc(E1,E2);
102 - x = (T *)kmem_alloc(E1,E2);
103 + x = kmem_zalloc(E1,E2);
105 - x = (T)kmem_alloc(E1,E2);
106 + x = (T)kmem_zalloc(E1,E2);
108 - x = devm_kmalloc(E2,E1,E3);
109 + x = devm_kzalloc(E2,E1,E3);
111 - x = (T *)devm_kmalloc(E2,E1,E3);
112 + x = devm_kzalloc(E2,E1,E3);
114 - x = (T)devm_kmalloc(E2,E1,E3);
115 + x = (T)devm_kzalloc(E2,E1,E3);
117 - x = kvmalloc(E1,E2);
118 + x = kvzalloc(E1,E2);
120 - x = (T *)kvmalloc(E1,E2);
121 + x = kvzalloc(E1,E2);
123 - x = (T)kvmalloc(E1,E2);
124 + x = (T)kvzalloc(E1,E2);
126 - x = kvmalloc_node(E1,E2,E3);
127 + x = kvzalloc_node(E1,E2,E3);
129 - x = (T *)kvmalloc_node(E1,E2,E3);
130 + x = kvzalloc_node(E1,E2,E3);
132 - x = (T)kvmalloc_node(E1,E2,E3);
133 + x = (T)kvzalloc_node(E1,E2,E3);
135 if ((x==NULL) || ...) S
136 - memset((T2)x,0,E1);
138 //----------------------------------------------------------
140 //----------------------------------------------------------
142 @r depends on org || report@
150 x = (T)kmalloc@p(E1,E2);
151 if ((x==NULL) || ...) S
154 @script:python depends on org@
160 msg_safe=msg.replace("[","@(").replace("]",")")
161 coccilib.org.print_todo(p[0], msg_safe)
163 @script:python depends on report@
168 msg="WARNING: kzalloc should be used for %s, instead of kmalloc/memset" % (x)
169 coccilib.report.print_report(p[0], msg)
171 //-----------------------------------------------------------------
172 @r1 depends on org || report@
180 x = (T)vmalloc@p(E1);
181 if ((x==NULL) || ...) S
184 @script:python depends on org@
190 msg_safe=msg.replace("[","@(").replace("]",")")
191 coccilib.org.print_todo(p[0], msg_safe)
193 @script:python depends on report@
198 msg="WARNING: vzalloc should be used for %s, instead of vmalloc/memset" % (x)
199 coccilib.report.print_report(p[0], msg)
201 //-----------------------------------------------------------------
202 @r2 depends on org || report@
205 expression E1,E2,E3,E4;
210 x = (T)dma_alloc_coherent@p(E2,E1,E3,E4);
211 if ((x==NULL) || ...) S
214 @script:python depends on org@
220 msg_safe=msg.replace("[","@(").replace("]",")")
221 coccilib.org.print_todo(p[0], msg_safe)
223 @script:python depends on report@
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)
231 //-----------------------------------------------------------------
232 @r3 depends on org || report@
240 x = (T)kmalloc_node@p(E1,E2,E3);
241 if ((x==NULL) || ...) S
244 @script:python depends on org@
250 msg_safe=msg.replace("[","@(").replace("]",")")
251 coccilib.org.print_todo(p[0], msg_safe)
253 @script:python depends on report@
258 msg="WARNING: kzalloc_node should be used for %s, instead of kmalloc_node/memset" % (x)
259 coccilib.report.print_report(p[0], msg)
261 //-----------------------------------------------------------------
262 @r4 depends on org || report@
270 x = (T)kmem_cache_alloc@p(E2,E3);
271 if ((x==NULL) || ...) S
274 @script:python depends on org@
280 msg_safe=msg.replace("[","@(").replace("]",")")
281 coccilib.org.print_todo(p[0], msg_safe)
283 @script:python depends on report@
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)
291 //-----------------------------------------------------------------
292 @r5 depends on org || report@
300 x = (T)kmem_alloc@p(E1,E2);
301 if ((x==NULL) || ...) S
304 @script:python depends on org@
310 msg_safe=msg.replace("[","@(").replace("]",")")
311 coccilib.org.print_todo(p[0], msg_safe)
313 @script:python depends on report@
318 msg="WARNING: kmem_zalloc should be used for %s, instead of kmem_alloc/memset" % (x)
319 coccilib.report.print_report(p[0], msg)
321 //-----------------------------------------------------------------
322 @r6 depends on org || report@
330 x = (T)devm_kmalloc@p(E2,E1,E3);
331 if ((x==NULL) || ...) S
334 @script:python depends on org@
340 msg_safe=msg.replace("[","@(").replace("]",")")
341 coccilib.org.print_todo(p[0], msg_safe)
343 @script:python depends on report@
348 msg="WARNING: devm_kzalloc should be used for %s, instead of devm_kmalloc/memset" % (x)
349 coccilib.report.print_report(p[0], msg)
351 //-----------------------------------------------------------------
352 @r7 depends on org || report@
360 x = (T)kvmalloc@p(E1,E2);
361 if ((x==NULL) || ...) S
364 @script:python depends on org@
370 msg_safe=msg.replace("[","@(").replace("]",")")
371 coccilib.org.print_todo(p[0], msg_safe)
373 @script:python depends on report@
378 msg="WARNING: kvzalloc should be used for %s, instead of kvmalloc/memset" % (x)
379 coccilib.report.print_report(p[0], msg)
381 //-----------------------------------------------------------------
382 @r9 depends on org || report@
390 x = (T)kvmalloc_node@p(E1,E2,E3);
391 if ((x==NULL) || ...) S
394 @script:python depends on org@
400 msg_safe=msg.replace("[","@(").replace("]",")")
401 coccilib.org.print_todo(p[0], msg_safe)
403 @script:python depends on report@
408 msg="WARNING: kvzalloc_node should be used for %s, instead of kvmalloc_node/memset" % (x)
409 coccilib.report.print_report(p[0], msg)