Debian Codegraph @

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

raw
1 /* PR middle-end/63272 - GCC should warn when using pointer to dead scoped
2 variable within the same function
3 Exercise -Wdangling-pointer for escaping stores of addreses of auto
4 variables.
5 { dg-do compile }
6 { dg-options "-O0 -Wall -ftrack-macro-expansion=0" } */
7
8 void* alloca (__SIZE_TYPE__);
9
10 void* sink (void*, ...);
11
12 extern void *evp;
13
14 void nowarn_store_extern_call (void)
15 {
16 int x;
17 evp = &x;
18 sink (0);
19 }
20
21 void nowarn_store_extern_ovrwrite (void)
22 {
23 int x;
24 evp = &x;
25 evp = 0;
26 }
27
28 void nowarn_store_extern_store (void)
29 {
30 int x;
31 void **p = (void**)sink (&evp);
32 evp = &x;
33 *p = 0;
34 }
35
36
37 void warn_store_alloca (int n)
38 {
39 // This fails because of a bug in the warning.
40 void *p = alloca (n);
41 evp = p; // { dg-warning "storing the address of local variable 'x' in 'evp1'" "pr??????" { xfail *-*-* } }
42 }
43
44
45 void warn_store_extern (void)
46 {
47 extern void *evp1; // { dg-message "'evp1' declared here" }
48 int x; // { dg-message "'x' declared here" }
49 evp1 = &x; // { dg-warning "storing the address of local variable 'x' in 'evp1'" }
50 }
51
52
53 void nowarn_store_arg_call (void **vpp)
54 {
55 int x;
56 *vpp = &x;
57 sink (0);
58 }
59
60 void nowarn_store_arg_ovrwrite (void **vpp)
61 {
62 int x;
63 *vpp = &x;
64 *vpp = 0;
65 }
66
67 void nowarn_store_arg_store (void **vpp)
68 {
69 int x;
70 void **p = (void**)sink (0);
71 *vpp = &x;
72 *p = 0;
73 }
74
75 void* nowarn_store_arg_store_arg (void **vpp1, void **vpp2)
76 {
77 int x; // { dg-message "'x' declared here" }
78 void **p = (void**)sink (0);
79 *vpp1 = &x; // { dg-warning "storing the address of local variable 'x' in '\\*vpp1'" }
80 *vpp2 = 0; // might overwrite *vpp1
81 return p;
82 }
83
84 void warn_store_arg (void **vpp)
85 {
86 int x; // { dg-message "'x' declared here" }
87 *vpp = &x; // { dg-warning "storing the address of local variable 'x' in '\\*vpp'" }
88 }
89
90