Debian Codegraph @

gcc/testsuite/c-c++-common/analyzer/analyzer-verbosity-2.c

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