1///
2/// Use kzalloc rather than kmalloc 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// URL: http://coccinelle.lip6.fr/rules/kzalloc.html
12// Options: --no-includes --include-headers
13//
14// Keywords: kmalloc, kzalloc
15// Version min: < 2.6.12 kmalloc
16// Version min:   2.6.14 kzalloc
17//
18
19virtual context
20virtual patch
21virtual org
22virtual report
23
24//----------------------------------------------------------
25//  For context mode
26//----------------------------------------------------------
27
28@depends on context@
29type T, T2;
30expression x;
31expression E1,E2;
32statement S;
33@@
34
35* x = (T)kmalloc(E1,E2);
36  if ((x==NULL) || ...) S
37* memset((T2)x,0,E1);
38
39//----------------------------------------------------------
40//  For patch mode
41//----------------------------------------------------------
42
43@depends on patch@
44type T, T2;
45expression x;
46expression E1,E2;
47statement S;
48@@
49
50- x = (T)kmalloc(E1,E2);
51+ x = kzalloc(E1,E2);
52  if ((x==NULL) || ...) S
53- memset((T2)x,0,E1);
54
55//----------------------------------------------------------
56//  For org mode
57//----------------------------------------------------------
58
59@r depends on org || report@
60type T, T2;
61expression x;
62expression E1,E2;
63statement S;
64position p;
65@@
66
67 x = (T)kmalloc@p(E1,E2);
68 if ((x==NULL) || ...) S
69 memset((T2)x,0,E1);
70
71@script:python depends on org@
72p << r.p;
73x << r.x;
74@@
75
76msg="%s" % (x)
77msg_safe=msg.replace("[","@(").replace("]",")")
78coccilib.org.print_todo(p[0], msg_safe)
79
80@script:python depends on report@
81p << r.p;
82x << r.x;
83@@
84
85msg="WARNING: kzalloc should be used for %s, instead of kmalloc/memset" % (x)
86coccilib.report.print_report(p[0], msg)
87