Debian Codegraph @

gcc/testsuite/c-c++-common/Wdangling-pointer.c

raw
1 /* PR middle-end/63272 - GCC should warn when using pointer to dead scoped
2 variable within the same function
3 Exercise basic cases of -Wdangling-pointer without optimization.
4 { dg-do compile }
5 { dg-options "-O0 -Wall -Wno-uninitialized -ftrack-macro-expansion=0" } */
6
7 typedef __INTPTR_TYPE__ intptr_t;
8 typedef __SIZE_TYPE__ size_t;
9
10 #if __cplusplus
11 # define EXTERN_C extern "C"
12 #else
13 # define EXTERN_C extern
14 #endif
15
16 EXTERN_C void* alloca (size_t);
17 EXTERN_C void* malloc (size_t);
18 EXTERN_C void* memchr (const void*, int, size_t);
19 EXTERN_C char* strchr (const char*, int);
20
21 int sink (const void*, ...);
22 #define sink(...) sink (0, __VA_ARGS__)
23
24 /* Verify that integer assignments don't cause bogus warnings.
25 Reduced from GFlibc's s_nextafter.c. */
26
27 int nowarn_integer (float x)
28 {
29 int i;
30
31 {
32 union
33 {
34 float x;
35 int i;
36 } u;
37
38 u.x = x;
39 i = u.i;
40 }
41
42 return i;
43 }
44
45 void nowarn_addr (void)
46 {
47 int *p;
48 {
49 int a[] = { 1, 2, 3 };
50 p = a;
51 }
52
53 // This is suspect but not a clear error.
54 sink (&p);
55 }
56
57
58 char* nowarn_ptr (void)
59 {
60 char *p;
61 sink (&p);
62 return p;
63 }
64
65
66 char* nowarn_cond_ptr (void)
67 {
68 // Distilled from a false positive in Glibc dlerror.c.
69 char *q;
70 if (sink (&q))
71 return q;
72
73 return 0;
74 }
75
76
77 void nowarn_loop_ptr (int n, int *p)
78 {
79 // Distilled from a false positive in Glibc td_thr_get_info.c.
80 for (int i = 0; i != 2; ++i)
81 {
82 int x;
83 sink (&x);
84 *p++ = x;
85 }
86 }
87
88
89 void nowarn_intptr_t (void)
90 {
91 intptr_t ip;
92 {
93 int a[] = { 1, 2, 3 };
94 ip = (intptr_t)a;
95 }
96
97 // Using an intptr_t is not diagnosed.
98 sink (0, ip);
99 }
100
101
102 void nowarn_string_literal (void)
103 {
104 const char *s;
105 {
106 s = "123";
107 }
108
109 sink (s);
110 }
111
112
113 void nowarn_extern_array (int x)
114 {
115 {
116 /* This is a silly sanity check. */
117 extern int eia[];
118 int *p;
119 {
120 p = eia;
121 }
122 sink (p);
123 }
124 }
125
126
127 void nowarn_static_array (int x)
128 {
129 {
130 const char *s;
131 {
132 static const char sca[] = "123";
133 s = sca;
134 }
135
136 sink (s);
137 }
138 {
139 const int *p;
140 {
141 static const int sia[] = { 1, 2, 3 };
142 p = sia;
143 }
144
145 sink (p);
146 }
147 {
148 const int *p;
149 {
150 static const int sia[] = { 1, 2, 3 };
151 p = (const int*)memchr (sia, x, sizeof sia);
152 }
153
154 sink (p);
155 }
156 }
157
158
159 void nowarn_alloca (unsigned n)
160 {
161 {
162 char *p;
163 {
164 p = (char*)alloca (n);
165 }
166 sink (p);
167 }
168 {
169 int *p;
170 {
171 p = (int*)alloca (n * sizeof *p);
172 sink (p);
173 }
174 sink (p);
175 }
176 {
177 long *p;
178 {
179 p = (long*)alloca (n * sizeof *p);
180 sink (p);
181 p = p + 1;
182 }
183 sink (p);
184 }
185 }
186
187
188 #pragma GCC diagnostic push
189 /* Verify that -Wdangling-pointer works with #pragma diagnostic. */
190 #pragma GCC diagnostic ignored "-Wdangling-pointer"
191
192 void nowarn_scalar_call_ignored (void *vp)
193 {
194 int *p;
195 {
196 int i;
197 p = &i;
198 }
199 sink (p);
200 }
201
202 #pragma GCC diagnostic pop
203
204
205 void* nowarn_return_local_addr (void)
206 {
207 int a[] = { 1, 2, 3 };
208 int *p = a;
209
210 /* This is a likely bug but it's not really one of using a dangling
211 pointer but rather of returning the address of a local variable
212 which is diagnosed by -Wreturn-local-addr. */
213 return p;
214 }
215
216 void* warn_return_local_addr (void)
217 {
218 int *p = 0;
219 {
220 int a[] = { 1, 2, 3 };
221 p = a;
222 }
223
224 /* Unlike the above case, here the pointer is dangling when it's
225 used. */
226 return p; // { dg-warning "using dangling pointer 'p' to 'a'" "array" }
227 }
228
229
230 void* nowarn_return_alloca (int n)
231 {
232 int *p = (int*)alloca (n);
233 sink (p);
234
235 /* This is a likely bug but it's not really one of using a dangling
236 pointer but rather of returning the address of a local variable
237 which is diagnosed by -Wreturn-local-addr. */
238 return p;
239 }
240
241
242 void warn_scalar_call (void)
243 {
244 int *p;
245 {
246 int i; // { dg-message "'i' declared" "note" }
247 p = &i;
248 }
249 sink (p); // { dg-warning "using dangling pointer 'p' to 'i'" "array" }
250 }
251
252
253 void warn_array_call (void)
254 {
255 int *p;
256 {
257 int a[] = { 1, 2, 3 }; // { dg-message "'a' declared" "note" }
258 p = a;
259 }
260 sink (p); // { dg-warning "using dangling pointer 'p' to 'a'" "array" }
261 }
262
263
264 void* warn_array_return (void)
265 {
266 int *p;
267 {
268 int a[] = { 1, 2, 3 }; // { dg-message "'a' declared" "note" }
269 p = a;
270 }
271 return p; // { dg-warning "using dangling pointer 'p' to 'a'" "array" }
272 }
273
274
275 void warn_pr63272_c1 (int i)
276 {
277 int *p = 0;
278
279 if (i)
280 {
281 int k = i; // { dg-message "'k' declared" "note" }
282 p = &k;
283 }
284
285 sink (p ? *p : 0); // { dg-warning "dangling pointer 'p' to 'k' may be used" }
286 }
287
288
289 void warn_pr63272_c4 (void)
290 {
291 int *p = 0;
292
293 {
294 int b; // { dg-message "'b' declared" "note" }
295 p = &b;
296 }
297
298 sink (p); // { dg-warning "using dangling pointer 'p' to 'b'" "scalar" }
299 }
300
301 void nowarn_cond_if (int i, int n)
302 {
303 int *p;
304 if (i)
305 {
306 int a[] = { 1, 2 };
307 p = a;
308 sink (p);
309 }
310 else
311 {
312 int *b = (int*)malloc (n);
313 p = b;
314 sink (p);
315 }
316
317 p = 0;
318 }
319
320
321 void warn_cond_if (int i, int n)
322 {
323 int *p;
324 if (i)
325 {
326 int a[] = { 1, 2 }; // { dg-message "'a' declared" "note" }
327 sink (a);
328 p = a;
329 }
330 else
331 {
332 int *b = (int*)malloc (n);
333 sink (b);
334 p = b;
335 }
336
337 sink (p); // { dg-warning "dangling pointer 'p' to 'a' may be used" }
338 }
339
340
341 void warn_cond_else (int i, int n)
342 {
343 int *p;
344 if (i)
345 {
346 int *a = (int*)malloc (n);
347 sink (a);
348 p = a;
349 }
350 else
351 {
352 int b[] = { 2, 3 };
353 sink (b);
354 p = b;
355 }
356
357 sink (p); // { dg-warning "dangling pointer 'p' to 'b' may be used" }
358 }
359
360
361 void warn_cond_if_else (int i)
362 {
363 int *p;
364 if (i)
365 {
366 int a[] = { 1, 2 }; // { dg-message "'a' declared" "note" }
367 sink (a);
368 p = a;
369 }
370 else
371 {
372 int b[] = { 3, 4 }; // { dg-message "'b' declared" "note" { xfail *-*-* } }
373 sink (b);
374 p = b;
375 }
376
377 /* With a PHI with more than invalid argument, only one use is diagnosed
378 because after the first diagnostic the code suppresses subsequent
379 ones for the same use. This needs to be fixed. */
380 sink (p); // { dg-warning "dangling pointer 'p' to 'a' may be used" }
381 // { dg-warning "dangling pointer 'p' to 'b' may be used" "pr??????" { xfail *-*-* } .-1 }
382 }
383
384
385 void nowarn_gcc_i386 (int i)
386 {
387 // Regression test reduced from gcc's i386.c.
388 char a[32], *p;
389
390 if (i != 1)
391 p = a;
392 else
393 p = 0;
394
395 if (i == 2)
396 sink (p);
397 else
398 {
399 if (p)
400 {
401 sink (p);
402 return;
403 }
404 sink (p);
405 }
406 }
407
408
409 void warn_memchr (char c1, char c2, char c3, char c4)
410 {
411 char *p = 0;
412 {
413 char a[] = { c1, c2, c3 };// { dg-message "'a' declared" "note" }
414 p = (char*)memchr (a, c4, 3);
415 if (!p)
416 return;
417 }
418
419 sink (p); // { dg-warning "using dangling pointer 'p' to 'a'" }
420 }
421
422
423 void warn_strchr (char c1, char c2, char c3, char c4)
424 {
425 char *p = 0;
426 {
427 char a[] = { c1, c2, c3 }; // { dg-message "'a' declared" "note" }
428 p = (char*)strchr (a, c4);
429 if (!p)
430 return;
431 }
432
433 sink (p); // { dg-warning "using dangling pointer 'p' to 'a'" }
434 }