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 -fno-tree-vectorize -Wall -Wno-array-bounds -fno-ipa-icf" } */
5
6 void sink (void*);
7
8 // Exercise trailing one-element array members.
9
10 struct A1
11 {
12 char n;
13 char a[1]; // { dg-message "destination object" "note" }
14 };
15
16 // Verify warning for access to a definition with an initializer that doesn't
17 // initialize the one-element array member.
18 struct A1 a1__ = { 0 };
19
20 void ga1__ (void)
21 {
22 a1__.a[0] = 0;
23 a1__.a[1] = 1; // { dg-warning "\\\[-Wstringop-overflow" }
24 a1__.a[2] = 2; // { dg-warning "\\\[-Wstringop-overflow" }
25
26 struct A1 a = { 1 };
27 a.a[0] = 0;
28 a.a[1] = 1; // { dg-warning "\\\[-Wstringop-overflow" }
29 a.a[2] = 2; // { dg-warning "\\\[-Wstringop-overflow" }
30 sink (&a);
31 }
32
33 // Verify warning for access to a definition with an initializer that
34 // initializes the one-element array member to empty.
35 struct A1 a1_0 = { 0, { } };
36
37 void ga1_0_ (void)
38 {
39 a1_0.a[0] = 0;
40 a1_0.a[1] = 1; // { dg-warning "\\\[-Wstringop-overflow" }
41 a1_0.a[2] = 2; // { dg-warning "\\\[-Wstringop-overflow" }
42
43 struct A1 a = { 1, { } };
44 a.a[0] = 0;
45 a.a[1] = 1; // { dg-warning "\\\[-Wstringop-overflow" }
46 a.a[2] = 2; // { dg-warning "\\\[-Wstringop-overflow" }
47 sink (&a);
48 }
49
50 // Verify warning for access to a definition with an initializer that
51 // initializes the one-element array member.
52 struct A1 a1_1 = { 0, { 1 } };
53
54 void ga1_1 (void)
55 {
56 a1_1.a[0] = 0;
57 a1_1.a[1] = 1; // { dg-warning "\\\[-Wstringop-overflow" }
58 a1_1.a[2] = 2; // { dg-warning "\\\[-Wstringop-overflow" }
59
60 struct A1 a = { 0, { 1 } };
61 a.a[0] = 0;
62 a.a[1] = 1; // { dg-warning "\\\[-Wstringop-overflow" }
63 a.a[2] = 2; // { dg-warning "\\\[-Wstringop-overflow" }
64 sink (&a);
65 }
66
67 // Exercise interior one-element array members (verify they're not
68 // treated as trailing.
69
70 struct A1i
71 {
72 char n;
73 char a[1]; // { dg-message "destination object" }
74 char x;
75 };
76
77 // Verify warning for access to a definition with an initializer that doesn't
78 // initialize the one-element array member.
79 struct A1i a1i__ = { 0 };
80
81 void ga1i__ (void)
82 {
83 a1i__.a[0] = 0;
84 a1i__.a[1] = 1; // { dg-warning "\\\[-Wstringop-overflow" }
85 a1i__.a[2] = 2; // { dg-warning "\\\[-Wstringop-overflow" }
86
87 struct A1i a = { 0 };
88 a.a[0] = 0;
89 a.a[1] = 1; // { dg-warning "\\\[-Wstringop-overflow" }
90 a.a[2] = 2; // { dg-warning "\\\[-Wstringop-overflow" }
91 sink (&a);
92 }
93
94 // Verify warning for access to a definition with an initializer that
95 // initializes the one-element array member to empty.
96 struct A1 a1i_0 = { 0, { } };
97
98 void ga1i_0_ (void)
99 {
100 a1i_0.a[0] = 0;
101 a1i_0.a[1] = 1; // { dg-warning "\\\[-Wstringop-overflow" }
102 a1i_0.a[2] = 2; // { dg-warning "\\\[-Wstringop-overflow" }
103
104 struct A1 a = { 0, { } };
105 a.a[0] = 0;
106 a.a[1] = 1; // { dg-warning "\\\[-Wstringop-overflow" }
107 a.a[2] = 2; // { dg-warning "\\\[-Wstringop-overflow" }
108 sink (&a);
109 }
110
111 // Verify warning for access to a definition with an initializer that
112 // initializes the one-element array member.
113 struct A1 a1i_1 = { 0, { 1 } };
114
115 void ga1i_1 (void)
116 {
117 a1i_1.a[0] = 0;
118 a1i_1.a[1] = 1; // { dg-warning "\\\[-Wstringop-overflow" }
119 a1i_1.a[2] = 2; // { dg-warning "\\\[-Wstringop-overflow" }
120
121 struct A1 a = { 0, { 1 } };
122 a.a[0] = 1;
123 a.a[1] = 2; // { dg-warning "\\\[-Wstringop-overflow" }
124 a.a[2] = 3; // { dg-warning "\\\[-Wstringop-overflow" }
125 sink (&a);
126 }