Debian Codegraph @

gcc/testsuite/gcc.dg/Warray-bounds-48.c

raw
1 /* PR middle-end/91647 - missing -Warray-bounds accessing a zero-length array
2 of a declared object
3 { dg-do "compile" }
4 { dg-options "-O2 -Wall" } */
5
6 /* pr102706: disabled warnings because the now-disabled conditions for the
7 bogus warnings to come up do not take cost analysis into account, and often
8 come up wrong. */
9 /* { dg-additional-options "-Wno-stringop-overflow" } */
10
11 typedef __INT16_TYPE__ int16_t;
12 typedef __INT32_TYPE__ int32_t;
13
14 void sink (void*);
15
16 /* Exercise a true flexible member. */
17
18 struct AX
19 {
20 int32_t n;
21 int16_t ax[]; // { dg-message "while referencing 'ax'" "member" }
22 };
23
24 static void warn_ax_local (struct AX *p)
25 {
26 p->ax[0] = 0; // { dg-warning "\\\[-Warray-bounds" }
27 p->ax[1] = 1; // { dg-warning "\\\[-Warray-bounds" }
28 }
29
30 static void nowarn_ax_extern (struct AX *p)
31 {
32 p->ax[0] = 0; p->ax[99] = 99; p->ax[999] = 999; p->ax[9999] = 9999;
33 }
34
35 static void warn_ax_local_buf (struct AX *p)
36 {
37 p->ax[0] = 4; p->ax[1] = 5; // { dg-bogus "\\\[-Wstringop-overflow" "pr102706" }
38 // { xfail { vect_slp_v2hi_store_align && { ! vect_slp_v4hi_store_unalign } } }
39
40 p->ax[2] = 6; // { dg-warning "\\\[-Warray-bounds" }
41 p->ax[3] = 7; // { dg-warning "\\\[-Warray-bounds" }
42 p->ax[4] = 8; // { dg-warning "\\\[-Warray-bounds" }
43 }
44
45 static void warn_ax_extern_buf (struct AX *p)
46 {
47 p->ax[0] = 9; p->ax[1] = 10; p->ax[2] = 11;
48
49 p->ax[3] = 12; // { dg-warning "\\\[-Warray-bounds" }
50 p->ax[4] = 13; // { dg-warning "\\\[-Warray-bounds" }
51 p->ax[5] = 14; // { dg-warning "\\\[-Warray-bounds" }
52 }
53
54 static void nowarn_ax_extern_bufx (struct AX *p)
55 {
56 p->ax[0] = 0; p->ax[99] = 99; p->ax[999] = 999; p->ax[9999] = 9999;
57 }
58
59 static void nowarn_ax_ref (struct AX *p)
60 {
61 p->ax[0] = 0; p->ax[99] = 99; p->ax[999] = 999; p->ax[9999] = 9999;
62 }
63
64 void test_ax (struct AX *p, unsigned n)
65 {
66 {
67 struct AX sax; // { dg-message "defined here" "struct definition" }
68 warn_ax_local (&sax);
69 sink (&sax);
70 }
71
72 {
73 extern
74 struct AX xsax;
75 nowarn_ax_extern (&xsax);
76 sink (&xsax);
77 }
78
79 {
80 /* Verify out-of-bounds access to the local BUF is diagnosed. */
81 char ax_buf_p2[sizeof (struct AX) + 2 * sizeof (int16_t)];
82 warn_ax_local_buf ((struct AX*) ax_buf_p2);
83 sink (ax_buf_p2);
84 }
85
86 {
87 /* Verify out-of-bounds access to the extern BUF with a known
88 bound is diagnosed. */
89 extern char ax_buf_p3[sizeof (struct AX) + 3 * sizeof (int16_t)];
90 warn_ax_extern_buf ((struct AX*) ax_buf_p3);
91 sink (ax_buf_p3);
92 }
93
94 {
95 /* Verify that accesses to BUFX with an unknown bound are not
96 diagnosed. */
97 extern char bufx[];
98 nowarn_ax_extern_bufx ((struct AX*) bufx);
99 sink (bufx);
100 }
101
102 {
103 /* Verify that accesses to BUFN with a runtime bound are not
104 diagnosed. */
105 char bufn[n];
106 nowarn_ax_extern_bufx ((struct AX*) bufn);
107 sink (bufn);
108 }
109
110 nowarn_ax_ref (p);
111 }
112
113
114 /* Exercise a zero-length trailing member array. It's the same as above
115 except that extern declarations with no definitions are considered to
116 have zero elements (they can't be initialized to have any). */
117
118 struct A0
119 {
120 int32_t n;
121 int16_t a0[0]; // { dg-message "while referencing 'a0'" "member" }
122 };
123
124 static void warn_a0_local (struct A0 *p)
125 {
126 p->a0[0] = 0; // { dg-warning "\\\[-Warray-bounds" }
127 p->a0[1] = 1; // { dg-warning "\\\[-Warray-bounds" }
128 }
129
130 static void warn_a0_extern (struct A0 *p)
131 {
132 p->a0[0] = 2; // { dg-warning "\\\[-Warray-bounds" }
133 p->a0[1] = 3; // { dg-warning "\\\[-Warray-bounds" }
134 }
135
136 static void warn_a0_local_buf (struct A0 *p)
137 {
138 p->a0[0] = 4; p->a0[1] = 5; // { dg-bogus "\\\[-Wstringop-overflow" "pr102706" }
139 // { xfail { vect_slp_v2hi_store_align && { ! vect_slp_v4hi_store_unalign } } }
140
141 p->a0[2] = 6; // { dg-warning "\\\[-Warray-bounds" }
142 p->a0[3] = 7; // { dg-warning "\\\[-Warray-bounds" }
143 p->a0[4] = 8; // { dg-warning "\\\[-Warray-bounds" }
144 }
145
146 static void warn_a0_extern_buf (struct A0 *p)
147 {
148 p->a0[0] = 9; p->a0[1] = 10; p->a0[2] = 11;
149
150 p->a0[3] = 12; // { dg-warning "\\\[-Warray-bounds" }
151 p->a0[4] = 13; // { dg-warning "\\\[-Warray-bounds" }
152 p->a0[5] = 14; // { dg-warning "\\\[-Warray-bounds" }
153 }
154
155 static void nowarn_a0_extern_bufx (struct A0 *p)
156 {
157 p->a0[0] = 0; p->a0[99] = 99; p->a0[999] = 999; p->a0[9999] = 9999;
158 }
159
160 static void nowarn_a0_ref (struct A0 *p)
161 {
162 p->a0[0] = 0; p->a0[99] = 99; p->a0[999] = 999; p->a0[9999] = 9999;
163 }
164
165 void test_a0 (struct A0 *p, unsigned n)
166 {
167 {
168 struct A0 sa0; // { dg-message "defined here" "struct definition" }
169 warn_a0_local (&sa0);
170 sink (&sa0);
171 }
172
173 {
174 extern
175 struct A0 xsa0; // { dg-message "defined here" "struct definition" }
176 warn_a0_extern (&xsa0);
177 sink (&xsa0);
178 }
179
180 {
181 /* Verify out-of-bounds access to the local BUF is diagnosed. */
182 char a0_buf_p2[sizeof (struct A0) + 2 * sizeof (int16_t)];
183 warn_a0_local_buf ((struct A0*) a0_buf_p2);
184 sink (a0_buf_p2);
185 }
186
187 {
188 /* Verify out-of-bounds access to the extern BUF with a known
189 bound is diagnosed. */
190 extern char a0_buf_p3[sizeof (struct A0) + 3 * sizeof (int16_t)];
191 warn_a0_extern_buf ((struct A0*) a0_buf_p3);
192 sink (a0_buf_p3);
193 }
194
195 {
196 /* Verify that accesses to BUFX with an unknown bound are not
197 diagnosed. */
198 extern char bufx[];
199 nowarn_a0_extern_bufx ((struct A0*) bufx);
200 sink (bufx);
201 }
202
203 {
204 /* Verify that accesses to BUFN with a runtime bound are not
205 diagnosed. */
206 char bufn[n];
207 nowarn_a0_extern_bufx ((struct A0*) bufn);
208 sink (bufn);
209 }
210
211 nowarn_a0_ref (p);
212 }
213
214
215 /* Exercise a one-element trailing member array. It's the same as above
216 except that it has exactly one element. */
217
218 struct A1
219 {
220 int32_t n;
221 __attribute__ ((aligned (4)))
222 int16_t a1[1]; // { dg-message "while referencing 'a1'" }
223 };
224
225 static void warn_a1_local_noinit (struct A1 *p)
226 {
227 p->a1[0] = 0;
228 p->a1[1] = 1; // { dg-warning "\\\[-Warray-bounds" }
229 p->a1[2] = 2; // { dg-warning "\\\[-Warray-bounds" }
230 }
231
232 static void warn_a1_extern (struct A1 *p)
233 {
234 p->a1[0] = 0;
235 p->a1[1] = 1; // { dg-warning "\\\[-Warray-bounds" }
236 p->a1[2] = 2; // { dg-warning "\\\[-Warray-bounds" }
237 }
238
239 static void warn_a1_init (struct A1 *p)
240 {
241 p->a1[0] = 0;
242 p->a1[1] = 1; // { dg-warning "\\\[-Warray-bounds" }
243 p->a1[2] = 2; // { dg-warning "\\\[-Warray-bounds" }
244 }
245
246 static void warn_a1_local_buf (struct A1 *p)
247 {
248 p->a1[0] = 0; p->a1[1] = 1; p->a1[2] = 2; p->a1[3] = 3;
249
250 p->a1[4] = 4; // { dg-warning "\\\[-Warray-bounds" }
251 }
252
253 static void warn_a1_extern_buf (struct A1 *p)
254 {
255 p->a1[0] = 0; p->a1[1] = 1; p->a1[2] = 2; p->a1[3] = 3; p->a1[4] = 4;
256
257 p->a1[5] = 5; // { dg-warning "\\\[-Warray-bounds" }
258 }
259
260 static void nowarn_a1_extern_bufx (struct A1 *p)
261 {
262 p->a1[0] = 0; p->a1[99] = 99; p->a1[999] = 999; p->a1[9999] = 9999;
263 }
264
265 static void nowarn_a1_ref (struct A1 *p)
266 {
267 p->a1[0] = 0; p->a1[99] = 99; p->a1[999] = 999; p->a1[9999] = 9999;
268 }
269
270 void test_a1 (struct A1 *p, unsigned n)
271 {
272 {
273 struct A1 a1;
274 warn_a1_local_noinit (&a1);
275 sink (&a1);
276 }
277
278 {
279 extern struct A1 a1x;
280 warn_a1_extern (&a1x);
281 sink (&a1x);
282 }
283 {
284 struct A1 a1 = { 0, { 1 } };
285 warn_a1_init (&a1);
286 sink (&a1);
287 }
288
289 {
290 /* Verify out-of-bounds access to the local BUF is diagnosed. */
291 char buf_p2[sizeof (struct A1) + 2 * sizeof (int16_t)];
292 warn_a1_local_buf ((struct A1*) buf_p2);
293 sink (buf_p2);
294 }
295
296 {
297 /* Verify out-of-bounds access to the extern BUF with a known
298 bound is diagnosed. */
299 extern char a1_buf_p3[sizeof (struct A1) + 3 * sizeof (int16_t)];
300 warn_a1_extern_buf ((struct A1*) a1_buf_p3);
301 sink (a1_buf_p3);
302 }
303
304 {
305 /* Verify that accesses to BUFX with an unknown bound are not
306 diagnosed. */
307 extern char bufx[];
308 nowarn_a1_extern_bufx ((struct A1*) bufx);
309 sink (bufx);
310 }
311
312 {
313 /* Verify that accesses to BUFN with a runtime bound are not
314 diagnosed. */
315 char bufn[n];
316 nowarn_a1_extern_bufx ((struct A1*) bufn);
317 sink (bufn);
318 }
319
320 nowarn_a1_ref (p);
321 }
322
323
324 /* Exercise a two-element trailing member array. It's treated
325 the same as an interior array member. */
326
327 struct A2
328 {
329 int32_t n;
330 int16_t a2[2]; // { dg-message "while referencing 'a2'" }
331 };
332
333 static void warn_a2_noinit (struct A2 *p)
334 {
335 p->a2[0] = 0; p->a2[1] = 1;
336
337 p->a2[2] = 2; // { dg-warning "\\\[-Warray-bounds" }
338 }
339
340 static void warn_a2_init (struct A2 *p)
341 {
342 p->a2[0] = 0; p->a2[1] = 1;
343
344 p->a2[2] = 2; // { dg-warning "\\\[-Warray-bounds" }
345 p->a2[9] = 9; // { dg-warning "\\\[-Warray-bounds" }
346 }
347
348 static void warn_a2_ref (struct A2 *p)
349 {
350 p->a2[0] = 0; p->a2[1] = 1;
351
352 p->a2[2] = 2; // { dg-warning "\\\[-Warray-bounds" }
353 p->a2[9] = 9; // { dg-warning "\\\[-Warray-bounds" }
354 }
355
356 void test_a2 (struct A2 *p)
357 {
358 {
359 struct A2 a2;
360 warn_a2_noinit (&a2);
361 sink (&a2);
362 }
363
364 {
365 struct A2 a2 = { 0, { 1, 2 } };
366 warn_a2_init (&a2);
367 sink (&a2);
368 }
369
370 warn_a2_ref (p);
371 }