145
|
1 /* PR c/71924 - missing -Wreturn-local-addr returning alloca result
|
|
2 { dg-do compile }
|
|
3 { dg-options "-O2 -Wall" } */
|
|
4
|
|
5 #define ATTR(...) __attribute__ ((__VA_ARGS__))
|
|
6
|
|
7 struct A { int a, b, c; };
|
|
8 struct B { int a, b, c[]; };
|
|
9
|
|
10 void sink (void*, ...);
|
|
11
|
|
12 ATTR (noipa) void*
|
|
13 return_alloca (int n)
|
|
14 {
|
|
15 void *p = __builtin_alloca (n);
|
|
16 sink (p);
|
|
17 return p; /* { dg-warning "function returns address of local" } */
|
|
18 }
|
|
19
|
|
20 ATTR (noipa) void*
|
|
21 return_alloca_index_cst (int n)
|
|
22 {
|
|
23 int *p = (int*)__builtin_alloca (n);
|
|
24 p = &p[1];
|
|
25 sink (p);
|
|
26 return p; /* { dg-warning "function returns address of local" } */
|
|
27 }
|
|
28
|
|
29 ATTR (noipa) void*
|
|
30 return_alloca_plus_cst (int n)
|
|
31 {
|
|
32 int *p = (int*)__builtin_alloca (n);
|
|
33 p += 1;
|
|
34 sink (p);
|
|
35 return p; /* { dg-warning "function returns address of local" } */
|
|
36 }
|
|
37
|
|
38 ATTR (noipa) void*
|
|
39 return_alloca_plus_var (int n, int i)
|
|
40 {
|
|
41 char *p = (char*)__builtin_alloca (n);
|
|
42 p += i;
|
|
43 sink (p);
|
|
44 return p; /* { dg-warning "function returns address of local" } */
|
|
45 }
|
|
46
|
|
47 ATTR (noipa) void*
|
|
48 return_alloca_member_1 (int n)
|
|
49 {
|
|
50 struct A *p = (struct A*)__builtin_alloca (n);
|
|
51 sink (&p->a);
|
|
52 return &p->a; /* { dg-warning "function returns address of local" } */
|
|
53 }
|
|
54
|
|
55 ATTR (noipa) void*
|
|
56 return_alloca_member_2 (int n)
|
|
57 {
|
|
58 struct A *p = (struct A*)__builtin_alloca (n);
|
|
59 sink (&p->b);
|
|
60 return &p->b; /* { dg-warning "function returns address of local" } */
|
|
61 }
|
|
62
|
|
63 ATTR (noipa) void*
|
|
64 return_alloca_flexarray (int n)
|
|
65 {
|
|
66 struct B *p = (struct B*)__builtin_alloca (n);
|
|
67 sink (p->c);
|
|
68 return p->c; /* { dg-warning "function returns address of local" } */
|
|
69 }
|
|
70
|
|
71
|
|
72 ATTR (noipa) void*
|
|
73 return_array (void)
|
|
74 {
|
|
75 int a[32];
|
|
76 void *p = a;
|
|
77 sink (p);
|
|
78 return p; /* { dg-warning "function returns address of local" } */
|
|
79 }
|
|
80
|
|
81 ATTR (noipa) void*
|
|
82 return_array_index_cst (void)
|
|
83 {
|
|
84 int a[32];
|
|
85 void *p = &a[2];
|
|
86 sink (p);
|
|
87 return p; /* { dg-warning "function returns address of local" } */
|
|
88 }
|
|
89
|
|
90 ATTR (noipa) void*
|
|
91 return_array_plus_cst (void)
|
|
92 {
|
|
93 int a[32];
|
|
94 void *p = a + 2;
|
|
95 sink (p);
|
|
96 return p; /* { dg-warning "function returns address of local" } */
|
|
97 }
|
|
98
|
|
99 ATTR (noipa) void*
|
|
100 return_array_plus_var (int i)
|
|
101 {
|
|
102 int a[32];
|
|
103 void *p = a + i;
|
|
104 sink (p);
|
|
105 return p; /* { dg-warning "function returns address of local" } */
|
|
106 }
|
|
107
|
|
108 ATTR (noipa) void*
|
|
109 return_array_member_1 (void)
|
|
110 {
|
|
111 struct A a[2];
|
|
112 int *p = &a[1].a;
|
|
113 sink (a, p);
|
|
114 return p; /* { dg-warning "function returns address of local" } */
|
|
115 }
|
|
116
|
|
117 ATTR (noipa) void*
|
|
118 return_array_member_2 (void)
|
|
119 {
|
|
120 struct A a[32];
|
|
121 int *p = &a[1].b;
|
|
122 sink (a, p);
|
|
123 return p; /* { dg-warning "function returns address of local" } */
|
|
124 }
|
|
125
|
|
126
|
|
127 ATTR (noipa) void*
|
|
128 return_vla (int n)
|
|
129 {
|
|
130 char a[n];
|
|
131 void *p = a;
|
|
132 sink (p);
|
|
133 return p; /* { dg-warning "function returns address of local" } */
|
|
134 }
|
|
135
|
|
136 ATTR (noipa) void*
|
|
137 return_vla_index_cst (int n)
|
|
138 {
|
|
139 char a[n];
|
|
140 char *p = &a[3];
|
|
141 sink (p);
|
|
142 return p; /* { dg-warning "function returns address of local" } */
|
|
143 }
|
|
144
|
|
145 ATTR (noipa) void*
|
|
146 return_vla_plus_cst (int n)
|
|
147 {
|
|
148 char a[n];
|
|
149 char *p = a + 3;
|
|
150 sink (p);
|
|
151 return p; /* { dg-warning "function returns address of local" } */
|
|
152 }
|
|
153
|
|
154 ATTR (noipa) void*
|
|
155 return_vla_index_var (int n, int i)
|
|
156 {
|
|
157 char a[n];
|
|
158 char *p = &a[i];
|
|
159 sink (p);
|
|
160 return p; /* { dg-warning "function returns address of local" } */
|
|
161 }
|
|
162
|
|
163 ATTR (noipa) void*
|
|
164 return_vla_plus_var (int n, int i)
|
|
165 {
|
|
166 char a[n];
|
|
167 char *p = a + i;
|
|
168 sink (p);
|
|
169 return p; /* { dg-warning "function returns address of local" } */
|
|
170 }
|
|
171
|
|
172 ATTR (noipa) void*
|
|
173 return_vla_member_1 (int n, int i)
|
|
174 {
|
|
175 struct A a[n];
|
|
176 void *p = &a[i].a;
|
|
177 sink (a, p);
|
|
178 return p; /* { dg-warning "function returns address of local" } */
|
|
179 }
|
|
180
|
|
181 ATTR (noipa) void*
|
|
182 return_vla_member_2 (int n, int i)
|
|
183 {
|
|
184 struct A a[n];
|
|
185 void *p = &a[i].b;
|
|
186 sink (a, p);
|
|
187 return p; /* { dg-warning "function returns address of local" } */
|
|
188 }
|
|
189
|
|
190
|
|
191 ATTR (noipa) void*
|
|
192 return_alloca_or_alloca (int n, int i)
|
|
193 {
|
|
194 void *p = i ? __builtin_alloca (n * i) : __builtin_alloca (n);
|
|
195 sink (p);
|
|
196 /* The warning here should really be "function returns". */
|
|
197 return p; /* { dg-warning "function (returns|may return) address of local" } */
|
|
198 }
|
|
199
|
|
200 ATTR (noipa) void*
|
|
201 return_alloca_or_alloca_2 (int n, int i)
|
|
202 {
|
|
203 void *p0 = __builtin_alloca (n);
|
|
204 void *p1 = __builtin_alloca (n * 2);
|
|
205 void *p = i ? p0 : p1;
|
|
206 sink (p0, p1, p);
|
|
207 /* Same as above. */
|
|
208 return p; /* { dg-warning "function (returns|may return) address of local" } */
|
|
209 }
|
|
210
|
|
211 ATTR (noipa) void*
|
|
212 return_array_or_array (int i)
|
|
213 {
|
|
214 int a[5];
|
|
215 int b[7];
|
|
216 void *p = i ? a : b;
|
|
217 sink (a, b, p);
|
|
218 /* The warning here should really be "function returns". */
|
|
219 return p; /* { dg-warning "function (returns|may return) address of local" } */
|
|
220 }
|
|
221
|
|
222 ATTR (noipa) void*
|
|
223 return_array_or_array_plus_var (int i, int j)
|
|
224 {
|
|
225 int a[5];
|
|
226 int b[7];
|
|
227
|
|
228 void *p0 = a + i;
|
|
229 void *p1 = b + j;
|
|
230
|
|
231 void *p = i < j ? p0 : p1;
|
|
232 sink (a, b, p0, p1, p);
|
|
233 /* The warning here should really be "function returns". */
|
|
234 return p; /* { dg-warning "function (returns|may return) address of local" } */
|
|
235 }
|
|
236
|
|
237 extern int global[32];
|
|
238
|
|
239 ATTR (noipa) void*
|
|
240 may_return_global_or_alloca (int n, int i)
|
|
241 {
|
|
242 void *p = i ? global : __builtin_alloca (n);
|
|
243 sink (p);
|
|
244 return p; /* { dg-warning "function may return address of local" } */
|
|
245 }
|
|
246
|
|
247
|
|
248 ATTR (noipa) void*
|
|
249 may_return_global_or_alloca_plus_cst (int n, int i)
|
|
250 {
|
|
251 int *p = i ? global : (int*)__builtin_alloca (n);
|
|
252 p += 7;
|
|
253 sink (p);
|
|
254 return p; /* { dg-warning "function may return address of local" } */
|
|
255 }
|
|
256
|
|
257 ATTR (noipa) void*
|
|
258 may_return_global_or_array (int n, int i)
|
|
259 {
|
|
260 int a[32];
|
|
261 void *p = i ? global : a;
|
|
262 sink (p);
|
|
263 return p; /* { dg-warning "function may return address of local" } */
|
|
264 }
|
|
265
|
|
266 ATTR (noipa) void*
|
|
267 may_return_global_or_array_plus_cst (int n, int i)
|
|
268 {
|
|
269 int a[32];
|
|
270 int *p = i ? global : a;
|
|
271 p += 4;
|
|
272 sink (p);
|
|
273 return p; /* { dg-warning "function may return address of local" } */
|
|
274 }
|
|
275
|
|
276 ATTR (noipa) void*
|
|
277 may_return_global_or_vla (int n, int i)
|
|
278 {
|
|
279 int a[n];
|
|
280 void *p = i ? global : a;
|
|
281 sink (p);
|
|
282 return p; /* { dg-warning "function may return address of local" } */
|
|
283 }
|
|
284
|
|
285 ATTR (noipa) void*
|
|
286 may_return_global_or_vla_plus_cst (int n, int i)
|
|
287 {
|
|
288 int a[n];
|
|
289 int *p = i ? global : a;
|
|
290 p += 4;
|
|
291 sink (p);
|
|
292 return p; /* { dg-warning "function may return address of local" } */
|
|
293 }
|