Debian Codegraph @

gcc/testsuite/c-c++-common/analyzer/malloc-paths-9-noexcept.c

raw
1 /* { dg-additional-options "-fdiagnostics-show-line-numbers -fdiagnostics-path-format=inline-events -fdiagnostics-show-caret -fno-exceptions" } */
2 /* { dg-skip-if "requires hosted libstdc++ for stdlib malloc" { ! hostedlib } } */
3 /* { dg-enable-nn-line-numbers "" } */
4
5 #include <stdlib.h>
6
7 void test_1 (void)
8 {
9 void *ptr = malloc (1024);
10 free (ptr);
11 free (ptr); /* { dg-warning "double-'free' of 'ptr'" } */
12 }
13 /* { dg-begin-multiline-output "" }
14 NN | free (ptr);
15 | ^~~~~~~~~~
16 'test_1': events 1-3
17 NN | void *ptr = malloc (1024);
18 | ^~~~~~~~~~~~~
19 | |
20 | (1) allocated here
21 NN | free (ptr);
22 | ~~~~~~~~~~
23 | |
24 | (2) first 'free' here
25 NN | free (ptr);
26 | ~~~~~~~~~~
27 | |
28 | (3) second 'free' here; first 'free' was at (2)
29 { dg-end-multiline-output "" { target c } } */
30 /* { dg-begin-multiline-output "" }
31 NN | free (ptr);
32 | ~~~~~^~~~~
33 'void test_1()': events 1-3
34 NN | void *ptr = malloc (1024);
35 | ~~~~~~~^~~~~~
36 | |
37 | (1) allocated here
38 NN | free (ptr);
39 | ~~~~~~~~~~
40 | |
41 | (2) first 'free' here
42 NN | free (ptr);
43 | ~~~~~~~~~~
44 | |
45 | (3) second 'free' here; first 'free' was at (2)
46 { dg-end-multiline-output "" { target c++ } } */
47
48 void test_2 (int x, int y)
49 {
50 void *ptr = malloc (1024);
51 if (x)
52 free (ptr);
53 if (y)
54 free (ptr); /* { dg-warning "double-'free' of 'ptr'" } */
55 } /* { dg-warning "leak of 'ptr'" } */
56
57 /* "double-'free' of 'ptr'". */
58 /* { dg-begin-multiline-output "" }
59 NN | free (ptr);
60 | ^~~~~~~~~~
61 'test_2': events 1-7
62 NN | void *ptr = malloc (1024);
63 | ^~~~~~~~~~~~~
64 | |
65 | (1) allocated here
66 NN | if (x)
67 | ~
68 | |
69 | (2) following 'true' branch (when 'x != 0')...
70 NN | free (ptr);
71 | ~~~~~~~~~~
72 | |
73 | (3) ...to here
74 | (4) first 'free' here
75 NN | if (y)
76 | ~
77 | |
78 | (5) following 'true' branch (when 'y != 0')...
79 NN | free (ptr);
80 | ~~~~~~~~~~
81 | |
82 | (6) ...to here
83 | (7) second 'free' here; first 'free' was at (4)
84 { dg-end-multiline-output "" { target c } } */
85 /* { dg-begin-multiline-output "" }
86 NN | free (ptr);
87 | ~~~~~^~~~~
88 'void test_2(int, int)': events 1-7
89 NN | void *ptr = malloc (1024);
90 | ~~~~~~~^~~~~~
91 | |
92 | (1) allocated here
93 NN | if (x)
94 | ~~
95 | |
96 | (2) following 'true' branch (when 'x != 0')...
97 NN | free (ptr);
98 | ~~~~~~~~~~
99 | |
100 | (3) ...to here
101 | (4) first 'free' here
102 NN | if (y)
103 | ~~
104 | |
105 | (5) following 'true' branch (when 'y != 0')...
106 NN | free (ptr);
107 | ~~~~~~~~~~
108 | |
109 | (6) ...to here
110 | (7) second 'free' here; first 'free' was at (4)
111 { dg-end-multiline-output "" { target c++ } } */
112
113 /* "leak of 'ptr'. */
114 /* { dg-begin-multiline-output "" }
115 NN | }
116 | ^
117 'test_2': events 1-6
118 NN | void *ptr = malloc (1024);
119 | ^~~~~~~~~~~~~
120 | |
121 | (1) allocated here
122 NN | if (x)
123 | ~
124 | |
125 | (2) following 'false' branch (when 'x == 0')...
126 NN | free (ptr);
127 NN | if (y)
128 | ~
129 | |
130 | (3) ...to here
131 | (4) following 'false' branch (when 'y == 0')...
132 NN | free (ptr);
133 NN | }
134 | ~
135 | |
136 | (5) ...to here
137 | (6) 'ptr' leaks here; was allocated at (1)
138 { dg-end-multiline-output "" { target c } } */
139 /* { dg-begin-multiline-output "" }
140 NN | }
141 | ^
142 'void test_2(int, int)': events 1-6
143 NN | void *ptr = malloc (1024);
144 | ~~~~~~~^~~~~~
145 | |
146 | (1) allocated here
147 NN | if (x)
148 | ~~
149 | |
150 | (2) following 'false' branch (when 'x == 0')...
151 NN | free (ptr);
152 NN | if (y)
153 | ~~
154 | |
155 | (3) ...to here
156 | (4) following 'false' branch (when 'y == 0')...
157 NN | free (ptr);
158 NN | }
159 | ~
160 | |
161 | (5) ...to here
162 | (6) 'ptr' leaks here; was allocated at (1)
163 { dg-end-multiline-output "" { target c++ } } */
164
165 int test_3 (int x, int y)
166 {
167 int *ptr = (int *)malloc (sizeof (int));
168 *ptr = 42; /* { dg-warning "dereference of possibly-NULL 'ptr'" } */
169 if (x)
170 free (ptr);
171
172 *ptr = 19; /* { dg-warning "use after 'free' of 'ptr'" } */
173 // TODO: two warnings here: one is from sm-malloc, the other from region model
174
175 if (y)
176 free (ptr); /* No double-'free' warning: we've already attempted
177 to dereference it above. */
178 return *ptr; /* { dg-warning "use after 'free' of 'ptr'" "use-after-free" } */
179 /* { dg-warning "leak of 'ptr'" "leak" { target *-*-* } .-1 } */
180 }
181
182 /* "dereference of possibly-NULL 'ptr'". */
183 /* { dg-begin-multiline-output "" }
184 NN | *ptr = 42;
185 | ~~~~~^~~~
186 'test_3': events 1-2
187 NN | int *ptr = (int *)malloc (sizeof (int));
188 | ^~~~~~~~~~~~~~~~~~~~~
189 | |
190 | (1) this call could return NULL
191 NN | *ptr = 42;
192 | ~~~~~~~~~
193 | |
194 | (2) 'ptr' could be NULL: unchecked value from (1)
195 { dg-end-multiline-output "" { target c } } */
196 /* { dg-begin-multiline-output "" }
197 NN | *ptr = 42;
198 | ~~~~~^~~~
199 'int test_3(int, int)': events 1-2
200 NN | int *ptr = (int *)malloc (sizeof (int));
201 | ~~~~~~~^~~~~~~~~~~~~~
202 | |
203 | (1) this call could return NULL
204 NN | *ptr = 42;
205 | ~~~~~~~~~
206 | |
207 | (2) 'ptr' could be NULL: unchecked value from (1)
208 { dg-end-multiline-output "" { target c++ } } */
209
210 /* "use after 'free' of 'ptr'". */
211 /* { dg-begin-multiline-output "" }
212 NN | *ptr = 19;
213 | ~~~~~^~~~
214 'test_3': events 1-6
215 NN | int *ptr = (int *)malloc (sizeof (int));
216 | ^~~~~~~~~~~~~~~~~~~~~
217 | |
218 | (1) allocated here
219 NN | *ptr = 42;
220 | ~~~~~~~~~
221 | |
222 | (2) assuming 'ptr' is non-NULL
223 NN | if (x)
224 | ~
225 | |
226 | (3) following 'true' branch (when 'x != 0')...
227 NN | free (ptr);
228 | ~~~~~~~~~~
229 | |
230 | (4) ...to here
231 | (5) freed here
232 NN |
233 NN | *ptr = 19;
234 | ~~~~~~~~~
235 | |
236 | (6) use after 'free' of 'ptr'; freed at (5)
237 { dg-end-multiline-output "" { target c } } */
238 /* { dg-begin-multiline-output "" }
239 NN | *ptr = 19;
240 | ~~~~~^~~~
241 'int test_3(int, int)': events 1-6
242 NN | int *ptr = (int *)malloc (sizeof (int));
243 | ~~~~~~~^~~~~~~~~~~~~~
244 | |
245 | (1) allocated here
246 NN | *ptr = 42;
247 | ~~~~~~~~~
248 | |
249 | (2) assuming 'ptr' is non-NULL
250 NN | if (x)
251 | ~~
252 | |
253 | (3) following 'true' branch (when 'x != 0')...
254 NN | free (ptr);
255 | ~~~~~~~~~~
256 | |
257 | (4) ...to here
258 | (5) freed here
259 NN |
260 NN | *ptr = 19;
261 | ~~~~~~~~~
262 | |
263 | (6) use after 'free' of 'ptr'; freed at (5)
264 { dg-end-multiline-output "" { target c++ } } */
265
266 /* "use after 'free' of 'ptr'". */
267 /* { dg-begin-multiline-output "" }
268 NN | return *ptr;
269 | ^~~~
270 'test_3': events 1-8
271 NN | int *ptr = (int *)malloc (sizeof (int));
272 | ^~~~~~~~~~~~~~~~~~~~~
273 | |
274 | (1) allocated here
275 NN | *ptr = 42;
276 | ~~~~~~~~~
277 | |
278 | (2) assuming 'ptr' is non-NULL
279 NN | if (x)
280 | ~
281 | |
282 | (3) following 'false' branch (when 'x == 0')...
283 ......
284 NN | *ptr = 19;
285 | ~~~~~~~~~
286 | |
287 | (4) ...to here
288 ......
289 NN | if (y)
290 | ~
291 | |
292 | (5) following 'true' branch (when 'y != 0')...
293 NN | free (ptr);
294 | ~~~~~~~~~~
295 | |
296 | (6) ...to here
297 | (7) freed here
298 NN |
299 NN | return *ptr;
300 | ~~~~
301 | |
302 | (8) use after 'free' of 'ptr'; freed at (7)
303 { dg-end-multiline-output "" { target c } } */
304 /* { dg-begin-multiline-output "" }
305 NN | return *ptr;
306 | ^~~
307 'int test_3(int, int)': events 1-8
308 NN | int *ptr = (int *)malloc (sizeof (int));
309 | ~~~~~~~^~~~~~~~~~~~~~
310 | |
311 | (1) allocated here
312 NN | *ptr = 42;
313 | ~~~~~~~~~
314 | |
315 | (2) assuming 'ptr' is non-NULL
316 NN | if (x)
317 | ~~
318 | |
319 | (3) following 'false' branch (when 'x == 0')...
320 ......
321 NN | *ptr = 19;
322 | ~~~~~~~~~
323 | |
324 | (4) ...to here
325 ......
326 NN | if (y)
327 | ~~
328 | |
329 | (5) following 'true' branch (when 'y != 0')...
330 NN | free (ptr);
331 | ~~~~~~~~~~
332 | |
333 | (6) ...to here
334 | (7) freed here
335 NN |
336 NN | return *ptr;
337 | ~~~
338 | |
339 | (8) use after 'free' of 'ptr'; freed at (7)
340 { dg-end-multiline-output "" { target c++ } } */
341
342 /* "leak of 'ptr'". */
343 /* { dg-begin-multiline-output "" }
344 NN | return *ptr;
345 | ^~~~
346 'test_3': events 1-7
347 NN | int *ptr = (int *)malloc (sizeof (int));
348 | ^~~~~~~~~~~~~~~~~~~~~
349 | |
350 | (1) allocated here
351 NN | *ptr = 42;
352 | ~~~~~~~~~
353 | |
354 | (2) assuming 'ptr' is non-NULL
355 NN | if (x)
356 | ~
357 | |
358 | (3) following 'false' branch (when 'x == 0')...
359 ......
360 NN | *ptr = 19;
361 | ~~~~~~~~~
362 | |
363 | (4) ...to here
364 ......
365 NN | if (y)
366 | ~
367 | |
368 | (5) following 'false' branch (when 'y == 0')...
369 ......
370 NN | return *ptr;
371 | ~~~~
372 | |
373 | (6) ...to here
374 | (7) 'ptr' leaks here; was allocated at (1)
375 { dg-end-multiline-output "" { target c } } */
376 /* { dg-begin-multiline-output "" }
377 NN | return *ptr;
378 | ^~~
379 'int test_3(int, int)': events 1-7
380 NN | int *ptr = (int *)malloc (sizeof (int));
381 | ~~~~~~~^~~~~~~~~~~~~~
382 | |
383 | (1) allocated here
384 NN | *ptr = 42;
385 | ~~~~~~~~~
386 | |
387 | (2) assuming 'ptr' is non-NULL
388 NN | if (x)
389 | ~~
390 | |
391 | (3) following 'false' branch (when 'x == 0')...
392 ......
393 NN | *ptr = 19;
394 | ~~~~~~~~~
395 | |
396 | (4) ...to here
397 ......
398 NN | if (y)
399 | ~~
400 | |
401 | (5) following 'false' branch (when 'y == 0')...
402 ......
403 NN | return *ptr;
404 | ~~~
405 | |
406 | (6) ...to here
407 | (7) 'ptr' leaks here; was allocated at (1)
408 { dg-end-multiline-output "" { target c++ } } */