1 /* PR middle-end/91458 - inconsistent warning for writing past the end
4 { dg-options "-O2 -Wall -Wno-array-bounds" } */
6 #define NOIPA __attribute__ ((noipa))
10 // Exercise flexible array members.
15 char a[]; // { dg-message "destination object 'Ax::a' of size 0" "note: flexarray" }
18 // Verify warning for a definition with no initializer.
23 ax_.a[0] = 0; // { dg-warning "\\\[-Wstringop-overflow" }
24 ax_.a[1] = 0; // { dg-warning "\\\[-Wstringop-overflow" "" { xfail { vect_slp_v2qi_store_unalign } } }
25 ax_.a[2] = 0; // { dg-warning "\\\[-Wstringop-overflow" }
28 // Verify warning for access to a definition with an initializer that doesn't
29 // initialize the flexible array member.
34 ax0.a[0] = 0; // { dg-warning "\\\[-Wstringop-overflow" }
35 ax0.a[1] = 0; // { dg-warning "\\\[-Wstringop-overflow" "" { xfail { vect_slp_v2qi_store_unalign } } }
36 ax0.a[2] = 0; // { dg-warning "\\\[-Wstringop-overflow" }
39 // Verify warning for access to a definition with an initializer that
40 // initializes the flexible array member to empty.
45 ax0_.a[0] = 0; // { dg-warning "\\\[-Wstringop-overflow" }
46 ax0_.a[1] = 0; // { dg-warning "\\\[-Wstringop-overflow" "" { xfail { vect_slp_v2qi_store_unalign } } }
47 ax0_.a[2] = 0; // { dg-warning "\\\[-Wstringop-overflow" }
50 // Verify warning for out-of-bounds accesses to a definition with
52 Ax ax1 = { 1, { 0 } };
56 ax1.a[0] = 0; // { dg-warning "\\\[-Wstringop-overflow" "" { target { vect_slp_v2qi_store_unalign } } }
57 ax1.a[1] = 0; // { dg-warning "\\\[-Wstringop-overflow" "" { xfail { vect_slp_v2qi_store_unalign } } }
58 ax1.a[2] = 0; // { dg-warning "\\\[-Wstringop-overflow" }
61 Ax ax2 = { 2, { 1, 0 } };
67 ax2.a[2] = 0; // { dg-warning "\\\[-Wstringop-overflow" }
71 // Verify no warning for an unknown struct object.
72 NOIPA void gaxp (Ax *p)
80 // Verify no warning for an extern struct object whose array may be
81 // initialized to any number of elements.
91 // Exercise zero-length array members.
96 char a[0]; // { dg-message "destination object 'A0::a' of size 0" "note: trailing zero-length array" }
99 // Verify warning for a definition with no initializer.
104 a0_.a[0] = 0; // { dg-warning "\\\[-Wstringop-overflow" }
105 a0_.a[1] = 0; // { dg-warning "\\\[-Wstringop-overflow" "" { xfail { vect_slp_v2qi_store_unalign } } }
106 a0_.a[2] = 0; // { dg-warning "\\\[-Wstringop-overflow" }
109 // Verify warning for access to a definition with an initializer that doesn't
110 // initialize the flexible array member.
115 a00.a[0] = 0; // { dg-warning "\\\[-Wstringop-overflow" }
116 a00.a[1] = 0; // { dg-warning "\\\[-Wstringop-overflow" "" { xfail { vect_slp_v2qi_store_unalign } } }
117 a00.a[2] = 0; // { dg-warning "\\\[-Wstringop-overflow" }
120 // Verify warning for access to a definition with an initializer that
121 // initializes the flexible array member to empty.
122 A0 a00_ = { 0, { } };
126 a00_.a[0] = 0; // { dg-warning "\\\[-Wstringop-overflow" }
127 a00_.a[1] = 0; // { dg-warning "\\\[-Wstringop-overflow" "" { xfail { vect_slp_v2qi_store_unalign } } }
128 a00_.a[2] = 0; // { dg-warning "\\\[-Wstringop-overflow" }
131 // The following are rejected with
132 // error: too many initializers for 'char [0]'
133 // A0 a01 = { 1, { 0 } };
134 // A0 a02 = { 2, { 1, 0 } };
137 // Verify no warning for an unknown struct object.
138 NOIPA void ga0p (A0 *p)
146 // Verify warning for an extern struct object which (unlike a true
147 // flexible array member) may not be initialized.
152 a0x.a[0] = 0; // { dg-warning "\\\[-Wstringop-overflow" }
153 a0x.a[3] = 0; // { dg-warning "\\\[-Wstringop-overflow" }
154 a0x.a[9] = 0; // { dg-warning "\\\[-Wstringop-overflow" }
158 // Exercise trailing one-element array members.
163 char a[1]; // { dg-message "at offset \[1-2\] into destination object 'A1::a' of size 1" "note: trailing one-element array" }
166 // Verify warning for a definition with no initializer.
171 a1_.a[0] = 0; // { dg-warning "\\\[-Wstringop-overflow" "" { target { vect_slp_v2qi_store_unalign } } }
172 a1_.a[1] = 0; // { dg-warning "\\\[-Wstringop-overflow" "" { xfail { vect_slp_v2qi_store_unalign } } }
173 a1_.a[2] = 0; // { dg-warning "\\\[-Wstringop-overflow" }
176 // Verify warning for access to a definition with an initializer that doesn't
177 // initialize the one-element array member.
182 a1__.a[0] = 0; // { dg-warning "\\\[-Wstringop-overflow" "" { target { vect_slp_v2qi_store_unalign } } }
183 a1__.a[1] = 0; // { dg-warning "\\\[-Wstringop-overflow" "" { xfail { vect_slp_v2qi_store_unalign } } }
184 a1__.a[2] = 0; // { dg-warning "\\\[-Wstringop-overflow" }
187 // Verify warning for access to a definition with an initializer that
188 // initializes the one-element array member to empty.
189 A1 a1_0 = { 0, { } };
193 a1_0.a[0] = 0; // { dg-warning "\\\[-Wstringop-overflow" "" { target { vect_slp_v2qi_store_unalign } } }
194 a1_0.a[1] = 0; // { dg-warning "\\\[-Wstringop-overflow" "" { xfail { vect_slp_v2qi_store_unalign } } }
195 a1_0.a[2] = 0; // { dg-warning "\\\[-Wstringop-overflow" }
198 // Verify warning for access to a definition with an initializer that
199 // initializes the one-element array member.
200 A1 a1_1 = { 0, { 1 } };
204 a1_1.a[0] = 0; // { dg-warning "\\\[-Wstringop-overflow" "" { target { vect_slp_v2qi_store_unalign } } }
205 a1_1.a[1] = 0; // { dg-warning "\\\[-Wstringop-overflow" "" { xfail { vect_slp_v2qi_store_unalign } } }
206 a1_1.a[2] = 0; // { dg-warning "\\\[-Wstringop-overflow" }
210 // Verify no warning for an unknown struct object.
211 NOIPA void ga1p (A1 *p)
219 // Verify warning for an extern struct object. Similar to the zero-length
220 // array case, a one-element trailing array can be initialized to at most
227 a1x.a[3] = 0; // { dg-warning "\\\[-Wstringop-overflow" }
228 a1x.a[9] = 0; // { dg-warning "\\\[-Wstringop-overflow" }
231 // Exercise interior one-element array members (verify they're not
232 // treated as trailing.
237 char a[1]; // { dg-message "at offset \[1-2\] into destination object 'A1i::a' of size 1" "note: interior one-element array" }
241 // Verify warning for a definition with no initializer.
247 a1i_.a[1] = 0; // { dg-warning "\\\[-Wstringop-overflow" "" { xfail { vect_slp_v2qi_store_unalign } } }
248 a1i_.a[2] = 0; // { dg-warning "\\\[-Wstringop-overflow" }
251 // Verify warning for access to a definition with an initializer that doesn't
252 // initialize the one-element array member.
258 a1i__.a[1] = 0; // { dg-warning "\\\[-Wstringop-overflow" "" { xfail { vect_slp_v2qi_store_unalign } } }
259 a1i__.a[2] = 0; // { dg-warning "\\\[-Wstringop-overflow" }
262 // Verify warning for access to a definition with an initializer that
263 // initializes the one-element array member to empty.
264 A1 a1i_0 = { 0, { } };
266 NOIPA void ga1i_0_ ()
268 a1i_0.a[0] = 0; // { dg-warning "\\\[-Wstringop-overflow" "" { target { vect_slp_v2qi_store_unalign } } }
269 a1i_0.a[1] = 0; // { dg-warning "\\\[-Wstringop-overflow" "" { xfail { vect_slp_v2qi_store_unalign } } }
270 a1i_0.a[2] = 0; // { dg-warning "\\\[-Wstringop-overflow" }
273 // Verify warning for access to a definition with an initializer that
274 // initializes the one-element array member.
275 A1 a1i_1 = { 0, { 1 } };
279 a1i_1.a[0] = 0; // { dg-warning "\\\[-Wstringop-overflow" "" { target { vect_slp_v2qi_store_unalign } } }
280 a1i_1.a[1] = 0; // { dg-warning "\\\[-Wstringop-overflow" "" { xfail { vect_slp_v2qi_store_unalign } } }
281 a1i_1.a[2] = 0; // { dg-warning "\\\[-Wstringop-overflow" }
285 // Verify no warning for an unknown struct object.
286 NOIPA void ga1ip (A1i *p)
289 p->a[3] = 0; // { dg-warning "\\\[-Wstringop-overflow" }
290 p->a[9] = 0; // { dg-warning "\\\[-Wstringop-overflow" }
294 // Verify no warning for an extern struct object.
300 a1ix.a[3] = 0; // { dg-warning "\\\[-Wstringop-overflow" }
301 a1ix.a[9] = 0; // { dg-warning "\\\[-Wstringop-overflow" }
305 // Verify non-POD classes with flexible array members.
310 char a[]; // { dg-message "destination object 'Bx::a' of size 0" "note: flexarray class member" }
312 // Verify the warning for a constant.
313 Bx () { a[0] = 0; } // { dg-warning "\\\[-Wstringop-overflow" }
315 // And also for a non-constant. Regardless of the subscript, the array
316 // of the object in function gxi() below has a zero size.
317 Bx (int i) { a[i] = 0; } // { dg-warning "\\\[-Wstringop-overflow" }
320 NOIPA void gbx (void)
326 NOIPA void gbxi (int i)
335 char a[0]; // { dg-message "destination object 'B0::a' of size 0" "note: zero-length trailing array class member" }
337 B0 () { a[0] = 0; } // { dg-warning "\\\[-Wstringop-overflow" }
341 NOIPA void gb0 (void)
351 char a[1]; // { dg-message "at offset 1 into destination object 'B1::a' of size 1" "note: one-element trailing array class member" }
353 B1 () { a[1] = 0; } // { dg-warning "\\\[-Wstringop-overflow" }
356 NOIPA void gb1 (void)
365 char a[123]; // { dg-message "at offset 123 into destination object 'B123::a' of size 123" "note: large trailing array class member" }
367 B123 () { a[123] = 0; } // { dg-warning "\\\[-Wstringop-overflow" }
370 NOIPA void gb123 (void)
379 char a[234]; // { dg-message "at offset 234 into destination object 'B234::a' of size 234" "note: large trailing array class member" }
381 B234 (int i) { a[i] = 0; } // { dg-warning "\\\[-Wstringop-overflow" }
384 NOIPA void g234 (void)
386 struct B234 b234 (234);