Debian Codegraph @

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