Fortran: Fix PR 47485.
[gcc.git] / gcc / testsuite / g++.dg / warn / Wstringop-overflow-3.C
blobe1d547fe4d40c4bbc8b7ec365abb1c32c6938e29
1 /* PR middle-end/91458 - inconsistent warning for writing past the end
2    of an array member
3    { dg-do compile }
4    { dg-options "-O2 -Wall -Wno-array-bounds" } */
6 #define NOIPA __attribute__ ((noipa))
8 void sink (void*);
10 // Exercise flexible array members.
12 struct Ax
14   char n;
15   char a[];                     // { dg-message "destination object 'Ax::a' of size 0" "note: flexarray" }
18 // Verify warning for a definition with no initializer.
19 Ax ax_;
21 NOIPA void gax_ ()
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.
30 Ax ax0 = { 0 };
32 NOIPA void gax0 ()
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.
41 Ax ax0_ = { 0, { } };
43 NOIPA void gax0_ ()
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
51 // an initializer.
52 Ax ax1 = { 1, { 0 } };
54 NOIPA void gax1 ()
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 } };
63 NOIPA void gax2 ()
65   ax2.a[0] = 0;
66   ax2.a[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)
74   p->a[0] = 0;
75   p->a[3] = 0;
76   p->a[9] = 0;
80 // Verify no warning for an extern struct object whose array may be
81 // initialized to any number of elements.
82 extern Ax axx;
84 NOIPA void gaxx ()
86   axx.a[0] = 0;
87   axx.a[3] = 0;
88   axx.a[9] = 0;
91 // Exercise zero-length array members.
93 struct A0
95   char n;
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.
100 A0 a0_;
102 NOIPA void ga0_ ()
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.
111 A0 a00 = { 0 };
113 NOIPA void ga00 ()
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, { } };
124 NOIPA void ga00_ ()
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)
140   p->a[0] = 0;
141   p->a[3] = 0;
142   p->a[9] = 0;
146 // Verify warning for an extern struct object which (unlike a true
147 // flexible array member) may not be initialized.
148 extern A0 a0x;
150 NOIPA void ga0x ()
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.
160 struct A1
162   char n;
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.
167 A1 a1_;
169 NOIPA void ga1_ ()
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.
178 A1 a1__ = { 0 };
180 NOIPA void ga1__ ()
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, { } };
191 NOIPA void ga1_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 } };
202 NOIPA void ga1_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)
213   p->a[0] = 0;
214   p->a[3] = 0;
215   p->a[9] = 0;
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
221 // a single element.
222 extern A1 a1x;
224 NOIPA void ga1x ()
226   a1x.a[0] = 0;
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.
234 struct A1i
236   char n;
237   char a[1];                    // { dg-message "at offset \[1-2\] into destination object 'A1i::a' of size 1" "note: interior one-element array" }
238   char x;
241 // Verify warning for a definition with no initializer.
242 A1i a1i_;
244 NOIPA void ga1i_ ()
246   a1i_.a[0] = 0;
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.
253 A1i a1i__ = { 0 };
255 NOIPA void ga1i__ ()
257   a1i__.a[0] = 0;
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 } };
277 NOIPA void ga1i_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)
288   p->a[0] = 0;
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.
295 extern A1i a1ix;
297 NOIPA void ga1ix ()
299   a1ix.a[0] = 0;
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.
307 struct Bx
309   char n;
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)
322   struct Bx bx;
323   sink (&bx);
326 NOIPA void gbxi (int i)
328   struct Bx bxi (i);
329   sink (&bxi);
332 struct B0
334   char n;
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)
343   struct B0 b0;
344   sink (&b0);
348 struct B1
350   char n;
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)
358   struct B1 b1;
359   sink (&b1);
363 struct B123
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)
372   struct B123 b123;
373   sink (&b123);
377 struct B234
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);
387   sink (&b234);