]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/testsuite/gcc.dg/Wzero-length-array-bounds-2.c
Update copyright years.
[thirdparty/gcc.git] / gcc / testsuite / gcc.dg / Wzero-length-array-bounds-2.c
CommitLineData
91eb5fa8
MS
1/* Test to verify that -Wzero-length-bounds and not -Warray-bounds is
2 issued for accesses to interior zero-length array members that are
3 within the bounds of the enclosing struct.
4 { dg-do compile }
5 { dg-options "-O2 -Wall" } */
6
7void sink (void*);
8
9struct A { int i; };
10struct B { int j; struct A a[0]; };
11
12struct C
13{
14 struct B b1;
15 struct B b2;
16};
17
18
19void test_B_ref (struct B *p)
20{
21 // References with negative indices are always diagnosed by -Warray-bounds
22 // even though they could be considered the same as past the end accesses
23 // to trailing zero-length arrays.
24 p->a[-1].i = 0; // { dg-warning "\\\[-Warray-bounds" }
25 p->a[ 0].i = 0;
26 p->a[ 1].i = 0;
27 sink (p);
28
29 p[1].a[-1].i = 0; // { dg-warning "\\\[-Warray-bounds" }
30 p[1].a[ 0].i = 0;
31 p[1].a[ 1].i = 0;
32}
33
34
35void test_C_ref (struct C *p)
36{
37 p->b1.a[-1].i = 0; // { dg-warning "\\\[-Warray-bounds" }
38 p->b1.a[ 0].i = 0; // { dg-warning "\\\[-Wzero-length-bounds" }
39 p->b1.a[ 1].i = 0; // { dg-warning "\\\[-Wzero-length-bounds" }
40
41 // Accesses to trailing zero-length arrays are not diagnosed (should
42 // they be?)
43 p->b2.a[ 0].i = 0;
44 p->b2.a[ 9].i = 0;
45}
46
47
48void test_C_decl (void)
49{
50 struct C c, *p = &c;
51
52 p->b1.a[-1].i = 0; // { dg-warning "\\\[-Warray-bounds" }
53
54 // c.b1.a[0].i overlaps c.b2.j.
55 p->b1.a[ 0].i = 0; // { dg-warning "\\\[-Wzero-length-bounds" }
56
57 // c.b1.a[1].i is past the end of c...
58 p->b1.a[ 1].i = 0; // { dg-warning "\\\[-Warray-bounds" }
59 sink (p);
60
61 // ...and so are references to all elements of c.b2.a
62 p->b2.a[ 0].i = 0; // { dg-warning "\\\[-Warray-bounds" }
63 p->b2.a[ 1].i = 0; // { dg-warning "\\\[-Warray-bounds" }
64 sink (p);
65}
66
67
68char cbuf1[1 * sizeof (struct C)];
69char cbuf2[2 * sizeof (struct C)] = { };
70
71void test_C_global_buf (void)
72{
73 struct C *p = (struct C*)&cbuf1;
74
75 p->b1.a[-1].i = 0; // { dg-warning "\\\[-Warray-bounds" }
76 p->b1.a[ 0].i = 0; // { dg-warning "\\\[-Wzero-length-bounds" }
77 p->b1.a[ 1].i = 0; // { dg-warning "\\\[-Warray-bounds" }
78 sink (p);
79
80 p->b2.a[ 0].i = 0; // { dg-warning "\\\[-Warray-bounds" }
81 p->b2.a[ 1].i = 0; // { dg-warning "\\\[-Warray-bounds" }
82 sink (p);
83
84 p = (struct C*)&cbuf2;
85 p->b1.a[-1].i = 0; // { dg-warning "\\\[-Warray-bounds" }
86 p->b1.a[ 0].i = 0; // { dg-warning "\\\[-Wzero-length-bounds" }
87 p->b1.a[ 1].i = 0; // { dg-warning "\\\[-Wzero-length-bounds" }
88 sink (p);
89
2e560abf 90 p->b2.a[ 0].i = 0; // { dg-warning "\\\[-Wstringop-overflow" "pr102706" { target { vect_slp_v2si_store_align && { ! vect_slp_v4si_store_unalign } } } }
91eb5fa8
MS
91 p->b2.a[ 1].i = 0;
92 p->b2.a[ 2].i = 0; // { dg-warning "\\\[-Warray-bounds" }
93 p->b2.a[ 3].i = 0; // { dg-warning "\\\[-Warray-bounds" }
94 sink (p);
95}
96
97
98void test_C_local_buf (void)
99{
100 char cbuf1[1 * sizeof (struct C)] = "";
101 char cbuf2[2 * sizeof (struct C)] = { };
102
103 struct C *p = (struct C*)&cbuf1;
104
105 p->b1.a[-1].i = 1; // { dg-warning "\\\[-Warray-bounds" }
106 p->b1.a[ 0].i = 2; // { dg-warning "\\\[-Wzero-length-bounds" }
107 p->b1.a[ 1].i = 3; // { dg-warning "\\\[-Warray-bounds" }
108 sink (p);
109
110 p->b2.a[ 0].i = 4; // { dg-warning "\\\[-Warray-bounds" }
111 p->b2.a[ 1].i = 5; // { dg-warning "\\\[-Warray-bounds" }
112 sink (p);
113
114 p = (struct C*)&cbuf2;
115 p->b1.a[-1].i = 6; // { dg-warning "\\\[-Warray-bounds" }
116 p->b1.a[ 0].i = 7; // { dg-warning "\\\[-Wzero-length-bounds" }
117 p->b1.a[ 1].i = 8; // { dg-warning "\\\[-Wzero-length-bounds" }
118 sink (p);
119
120 p->b2.a[ 0].i = 9;
121 p->b2.a[ 1].i = 10;
122 p->b2.a[ 2].i = 11; // { dg-warning "\\\[-Warray-bounds" }
123 p->b2.a[ 3].i = 12; // { dg-warning "\\\[-Warray-bounds" }
124 sink (p);
125}