1 /* PR middle-end/63272 - GCC should warn when using pointer to dead scoped
2 variable within the same function
3 Exercise conditional uses dangling pointers with optimization.
4 { dg-do compile }
5 { dg-options "-O2 -Wall -Wno-maybe-uninitialized" } */
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* memcpy (void*, const void*, size_t);
17
18 void sink (const void*, ...);
19
20 char* nowarn_conditional (char *s)
21 {
22 // Reduced from Glibc's tmpnam.c.
23 extern char a[5];
24 char b[5];
25 char *p = s ? s : b;
26
27 sink (p);
28
29 if (s == 0)
30 return a;
31
32 return s;
33 }
34
35
36 char* nowarn_conditional_memcpy (char *s)
37 {
38 // Reduced from Glibc's tmpnam.c.
39 extern char a[5];
40 char b[5];
41 char *p = s ? s : b;
42
43 sink (p);
44
45 if (s == 0)
46 return (char*)memcpy (a, p, 5);
47
48 return s;
49 }
50
51
52 int warn_conditional_block (int i)
53 {
54 int *p;
55 if (i)
56 {
57 int a[] = { 1, 2, 3 };
58 p = &a[i];
59 }
60 else
61 p = &i;
62
63 return *p; // { dg-warning "dangling pointer \('p' \)to 'a' may be used" }
64 }