Debian Codegraph @

demos/janitorings/kzalloc.cocci

raw
1 // script on KJ:
2 // You can probably find examples of that by running:
3 //
4 // $ grep -Er -B10 "memset ?\(.*,0 ?," * | less
5 //
6 // and searching for the string "kmalloc" in the output.
7
8
9
10
11 // have to duplicate a lot of rules because T E only matches if E has a known
12 // type, even if T is not used elsewhere.
13
14 // originally, the whens were when != x, but that doesn't work because x
15 // only binds to the outermost expression, not all possible expressions, and
16 // the value returned by kmalloc is usually used as a subexpression.
17 // so we have considered the typical uses that may cause problems; a function
18 // call or dereference
19
20 //\(x->fld\|f(...,x,...)\|x=E\)
21
22 @@
23 type T2;
24 expression x;
25 identifier f,fld;
26 expression E;
27 expression E1,E2;
28 expression e1,e2,e3,y;
29 statement S;
30 @@
31
32 x =
33 - kmalloc
34 + kzalloc
35 (E1,E2)
36 ... when != \(x->fld=E;\|y=f(...,x,...);\|f(...,x,...);\|x=E;\|while(...) S\|for(e1;e2;e3) S\)
37 - memset((T2)x,0,E1);
38
39 @@
40 type T2;
41 type T1;
42 T1 *x;
43 identifier f,fld;
44 expression E;
45 expression E2;
46 expression e1,e2,e3,y;
47 statement S;
48 @@
49
50 x =
51 - kmalloc
52 + kzalloc
53 (sizeof(T1),E2)
54 ... when != \(x->fld=E;\|y=f(...,x,...);\|f(...,x,...);\|x=E;\|while(...) S\|for(e1;e2;e3) S\)
55 - memset((T2)x,0,sizeof(*x));
56
57 @@
58 type T2;
59 type T1;
60 T1 *x;
61 identifier f,fld;
62 expression E;
63 expression E2;
64 expression e1,e2,e3,y;
65 statement S;
66 @@
67
68 x =
69 - kmalloc
70 + kzalloc
71 (sizeof(*x),E2)
72 ... when != \(x->fld=E;\|y=f(...,x,...);\|f(...,x,...);\|x=E;\|while(...) S\|for(e1;e2;e3) S\)
73 - memset((T2)x,0,sizeof(T1));
74
75 // ---------------------------------------------------------------------
76 // ---------------------------------------------------------------------
77 @@
78 type T2;
79 expression x;
80 identifier f,fld;
81 expression E;
82 expression E1,E2;
83 expression e1,e2,e3,y;
84 statement S, S1;
85 @@
86
87 x =
88 - kmalloc
89 + kzalloc
90 (E1,E2)
91 ... when != \(x->fld=E;\|y=f(...,x,...);\|f(...,x,...);\|x=E;\|while(...) S\|for(e1;e2;e3) S\)
92 if(x != NULL) {
93 ... when != \(x->fld=E;\|y=f(...,x,...);\|f(...,x,...);\|x=E;\|while(...) S\|for(e1;e2;e3) S\)
94 - memset((T2)x,0,E1);
95 ...
96 } else S1
97
98 @@
99 type T2;
100 type T1;
101 T1 *x;
102 identifier f,fld;
103 expression E;
104 expression E2;
105 expression e1,e2,e3,y;
106 statement S, S1;
107 @@
108
109 x =
110 - kmalloc
111 + kzalloc
112 (sizeof(T1),E2)
113 ... when != \(x->fld=E;\|y=f(...,x,...);\|f(...,x,...);\|x=E;\|while(...) S\|for(e1;e2;e3) S\)
114 if(x != NULL) {
115 ... when != \(x->fld=E;\|y=f(...,x,...);\|f(...,x,...);\|x=E;\|while(...) S\|for(e1;e2;e3) S\)
116 - memset((T2)x,0,sizeof(*x));
117 ...
118 } else S1
119
120 @@
121 type T2;
122 type T1;
123 T1 *x;
124 identifier f,fld;
125 expression E;
126 expression E2;
127 expression e1,e2,e3,y;
128 statement S, S1;
129 @@
130
131 x =
132 - kmalloc
133 + kzalloc
134 (sizeof(*x),E2)
135 ... when != \(x->fld=E;\|y=f(...,x,...);\|f(...,x,...);\|x=E;\|while(...) S\|for(e1;e2;e3) S\)
136 if(x != NULL) {
137 ... when != \(x->fld=E;\|y=f(...,x,...);\|f(...,x,...);\|x=E;\|while(...) S\|for(e1;e2;e3) S\)
138 - memset((T2)x,0,sizeof(T1));
139 ...
140 } else S1
141
142 // ---------------------------------------------------------------------
143 // ---------------------------------------------------------------------
144 @@
145 type T2;
146 type T1;
147 identifier x;
148 identifier f,fld;
149 expression E;
150 expression E1,E2;
151 expression e1,e2,e3,y;
152 statement S;
153 @@
154
155 T1 x =
156 - kmalloc
157 + kzalloc
158 (E1,E2);
159 ... when != \(x->fld=E;\|y=f(...,x,...);\|f(...,x,...);\|x=E;\|while(...) S\|for(e1;e2;e3) S\)
160 - memset((T2)x,0,E1);
161
162 @@
163 type T2;
164 type T1;
165 identifier x;
166 identifier f,fld;
167 expression E;
168 expression E2;
169 expression e1,e2,e3,y;
170 statement S;
171 @@
172
173 T1 x =
174 - kmalloc
175 + kzalloc
176 (sizeof(T1),E2);
177 ... when != \(x->fld=E;\|y=f(...,x,...);\|f(...,x,...);\|x=E;\|while(...) S\|for(e1;e2;e3) S\)
178 - memset((T2)x,0,sizeof(*x));
179
180 @@
181 type T2;
182 type T1;
183 identifier x;
184 identifier f,fld;
185 expression E;
186 expression E2;
187 expression e1,e2,e3,y;
188 statement S;
189 @@
190
191 T1 x =
192 - kmalloc
193 + kzalloc
194 (sizeof(*x),E2);
195 ... when != \(x->fld=E;\|y=f(...,x,...);\|f(...,x,...);\|x=E;\|while(...) S\|for(e1;e2;e3) S\)
196 - memset((T2)x,0,sizeof(T1));
197
198 // ---------------------------------------------------------------------
199 // ---------------------------------------------------------------------
200 @@
201 type T2;
202 type T1;
203 identifier x;
204 identifier f,fld;
205 expression E;
206 expression E1,E2;
207 expression e1,e2,e3,y;
208 statement S, S1;
209 @@
210
211 T1 x =
212 - kmalloc
213 + kzalloc
214 (E1,E2);
215 ... when != \(x->fld=E;\|y=f(...,x,...);\|f(...,x,...);\|x=E;\|while(...) S\|for(e1;e2;e3) S\)
216 if(x != NULL) {
217 ... when != \(x->fld=E;\|y=f(...,x,...);\|f(...,x,...);\|x=E;\|while(...) S\|for(e1;e2;e3) S\)
218 - memset((T2)x,0,E1);
219 ...
220 } else S1
221
222 @@
223 type T2;
224 type T1;
225 identifier x;
226 identifier f,fld;
227 expression E;
228 expression E2;
229 expression e1,e2,e3,y;
230 statement S, S1;
231 @@
232
233 T1 x =
234 - kmalloc
235 + kzalloc
236 (sizeof(T1),E2);
237 ... when != \(x->fld=E;\|y=f(...,x,...);\|f(...,x,...);\|x=E;\|while(...) S\|for(e1;e2;e3) S\)
238 if(x != NULL) {
239 ... when != \(x->fld=E;\|y=f(...,x,...);\|f(...,x,...);\|x=E;\|while(...) S\|for(e1;e2;e3) S\)
240 - memset((T2)x,0,sizeof(*x));
241 ...
242 } else S1
243
244 @@
245 type T2;
246 type T1;
247 identifier x;
248 identifier f,fld;
249 expression E;
250 expression E2;
251 expression e1,e2,e3,y;
252 statement S, S1;
253 @@
254
255 T1 x =
256 - kmalloc
257 + kzalloc
258 (sizeof(*x),E2);
259 ... when != \(x->fld=E;\|y=f(...,x,...);\|f(...,x,...);\|x=E;\|while(...) S\|for(e1;e2;e3) S\)
260 if(x != NULL) {
261 ... when != \(x->fld=E;\|y=f(...,x,...);\|f(...,x,...);\|x=E;\|while(...) S\|for(e1;e2;e3) S\)
262 - memset((T2)x,0,sizeof(T1));
263 ...
264 } else S1
265
266 // ---------------------------------------------------------------------
267 // ---------------------------------------------------------------------
268 @@
269 expression E1,E2,E3;
270 @@
271
272 - kzalloc(E1 * E2,E3)
273 + kcalloc(E1,E2,E3)