Debian Codegraph @

gcc/testsuite/gcc.dg/Wzero-length-array-bounds.c

raw
1 /* PR middle-end/91647 - missing -Warray-bounds accessing a zero-length array
2 of a declared object
3 Test to exercise -Wzero-length-bounds.
4 { dg-do compile }
5 { dg-options "-O2 -Wall" } */
6
7 void sink (void*);
8
9 struct X { int a[0]; int b, c; };
10
11 extern struct X x;
12
13 void bad (int i, int j)
14 {
15 x.a[0] = 0; // { dg-warning "\\\[-Wzero-length-bounds" }
16 x.a[1] = 1; // { dg-warning "\\\[-Wzero-length-bounds" }
17 x.a[2] = 2; // { dg-warning "\\\[-Warray-bounds" }
18
19 x.a[i] = 3; // { dg-warning "\\\[-Wzero-length-bounds" }
20 x.a[j] = 4; // { dg-warning "array subscript 'j' is outside the bounds of an interior zero-length array" }
21 }
22
23 void access_by_reference (struct X *p, int i)
24 {
25 p->a[0] = 0; // { dg-warning "\\\[-Wzero-length-bounds" }
26 p->a[1] = 0; // { dg-warning "\\\[-Wzero-length-bounds" }
27 p->a[2] = 0; // { dg-warning "\\\[-Wzero-length-bounds" }
28 p->a[i] = 0; // { dg-warning "\\\[-Wzero-length-bounds" }
29 }
30
31
32 extern struct X a[2];
33
34 void access_to_array (int i)
35 {
36 a[0].a[0] = 0; // { dg-warning "\\\[-Wzero-length-bounds" }
37 a[0].a[1] = 1; // { dg-warning "\\\[-Wzero-length-bounds" }
38 /* Accesses to a subsequent element of the enclosing array seem like
39 a more sever problem than those to the next member of the same
40 struct and so might perhaps be better diagnosed by -Warray-bounds.
41 Then again, code that does this sort of crap might as well get what
42 it deserves if it disables -Wzero-length-bounds. */
43 a[0].a[2] = 2; // { dg-warning "\\\[-Wzero-length-bounds" }
44
45 a[0].a[i] = 3; // { dg-warning "\\\[-Wzero-length-bounds" }
46 sink (a);
47
48 a[1].a[0] = 4; // { dg-warning "\\\[-Wzero-length-bounds" }
49 a[1].a[1] = 5; // { dg-warning "\\\[-Wzero-length-bounds" }
50 a[1].a[2] = 6; // { dg-warning "\\\[-Warray-bounds" }
51
52 a[1].a[i] = 7; // { dg-warning "\\\[-Wzero-length-bounds" }
53 sink (a);
54
55 a[i].a[0] = 8; // { dg-warning "\\\[-Wzero-length-bounds" }
56 a[i].a[1] = 9; // { dg-warning "\\\[-Wzero-length-bounds" }
57 a[i].a[2] = 0; // { dg-warning "\\\[-Wzero-length-bounds" }
58 }
59
60
61 struct Y
62 {
63 struct X a[2], b;
64 int c;
65 };
66
67 extern struct Y y;
68
69 void access_to_member (int i)
70 {
71 y.a[0].a[0] = 0; // { dg-warning "\\\[-Wzero-length-bounds" }
72 y.a[0].a[1] = 0; // { dg-warning "\\\[-Wzero-length-bounds" }
73 y.a[0].a[2] = 0; // { dg-warning "\\\[-Wzero-length-bounds" }
74 sink (a);
75
76 y.a[1].a[0] = 0; // { dg-warning "\\\[-Wzero-length-bounds" }
77 y.a[1].a[1] = 0; // { dg-warning "\\\[-Wzero-length-bounds" }
78 /* Similar to the array case above, accesses to a subsequent member
79 of the "parent" struct seem like a more severe problem than those
80 to the next member of the same struct. */
81 y.a[1].a[2] = 0; // { dg-warning "\\\[-Wzero-length-bounds" }
82 sink (a);
83
84 y.b.a[0] = 0; // { dg-warning "\\\[-Wzero-length-bounds" }
85 y.b.a[1] = 0; // { dg-warning "\\\[-Wzero-length-bounds" }
86 y.b.a[2] = 0; // { dg-warning "\\\[-Wzero-length-bounds" }
87 y.b.a[3] = 0; // { dg-warning "\\\[-Warray-bounds" }
88 }