Debian Codegraph @

gcc/testsuite/g++.dg/warn/Warray-bounds-18.C

raw
1 /* PR middle-end/98266 - bogus array subscript is partly outside array
2 bounds on virtual inheritance
3 { dg-do compile }
4 { dg-options "-O2 -Wall" } */
5
6 struct A
7 {
8 int ai, aj, aa[2];
9
10 virtual ~A ();
11 };
12
13 struct B: virtual A { };
14 struct C: virtual A { };
15
16 void sink (void*);
17
18 struct C1: virtual A
19 {
20 int c2i, c2j, c2a[2];
21
22 C1 ();
23 ~C1 ()
24 { // { dg-bogus "\\\[-Warray-bounds" }
25 c2i = __LINE__; // { dg-bogus "\\\[-Warray-bounds" }
26 c2j = __LINE__; // { dg-bogus "\\\[-Warray-bounds" }
27 c2a[0] = __LINE__; // { dg-bogus "\\\[-Warray-bounds" }
28 c2a[1] = __LINE__; // { dg-bogus "\\\[-Warray-bounds" }
29 c2a[2] = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
30 }
31 };
32
33 struct D1: virtual B, virtual C1
34 {
35 D1 ();
36 };
37
38 void sink (void*);
39
40 /* Verify that only out of bounds accesses to members of an ordinary base
41 class are diagnosed. Use direct array accesses. */
42 void test_vmem_base_ctor_arryaccess ()
43 {
44 D1 d2;
45 sink (&d2);
46 }
47
48
49 struct C2: virtual A
50 {
51 int c3a[2];
52
53 C2 ();
54 ~C2 ()
55 { // { dg-bogus "\\\[-Warray-bounds" }
56 int *p = c3a;
57 *p++ = __LINE__;
58 *p++ = __LINE__;
59 *p++ = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
60 }
61 };
62
63 struct D2: virtual B, virtual C2
64 {
65 D2 ();
66 };
67
68 /* Verify that only out of bounds accesses to members of an ordinary base
69 class are diagnosed. Use pointer accesses. */
70 void test_vmem_base_dtor_ptraccess ()
71 {
72 D2 d3;
73 sink (&d3);
74 }
75
76
77 struct C3: virtual A // { dg-bogus "\\\[-Warray-bounds" }
78 {
79 int i, j, a[2];
80
81 C3 ();
82 };
83
84 struct D3: virtual B, virtual C3
85 {
86 D3 ()
87 { // { dg-bogus "\\\[-Warray-bounds" }
88 i = __LINE__; // { dg-bogus "\\\[-Warray-bounds" }
89 j = __LINE__; // { dg-bogus "\\\[-Warray-bounds" }
90 a[0] = __LINE__; // { dg-bogus "\\\[-Warray-bounds" }
91 a[1] = __LINE__; // { dg-bogus "\\\[-Warray-bounds" }
92 a[2] = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
93 }
94 };
95
96 /* Verify that only out of bounds accesses to members of an ordinary base
97 class made in the ctor of a derived class are diagnosed. Use direct
98 array accesses. */
99 void test_vmem_derived_ctor_arryaccess ()
100 {
101 D3 d4;
102 sink (&d4);
103 }
104
105
106 struct D4: virtual B, virtual C3
107 {
108 D4 ()
109 { // { dg-bogus "\\\[-Warray-bounds" }
110 int *p = a;
111 *p++ = __LINE__;
112 *p++ = __LINE__;
113 *p++ = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
114 }
115 };
116
117 /* Verify that only out of bounds accesses to members of an ordinary base
118 class made in the ctor of a derived class are diagnosed. Use pointer
119 accesses. */
120 void test_vmem_derived_ctor_ptraccess ()
121 {
122 D4 d5;
123 sink (&d5);
124 }
125
126
127 struct D5: virtual B, virtual C3 // { dg-bogus "\\\[-Warray-bounds" }
128 {
129 ~D5 ()
130 {
131 i = __LINE__; // { dg-bogus "\\\[-Warray-bounds" }
132 j = __LINE__; // { dg-bogus "\\\[-Warray-bounds" }
133 a[0] = __LINE__; // { dg-bogus "\\\[-Warray-bounds" }
134 a[1] = __LINE__; // { dg-bogus "\\\[-Warray-bounds" }
135 a[2] = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
136 }
137 };
138
139 /* Verify that only out of bounds accesses to members of an ordinary base
140 class made in the dtor of a derived class are diagnosed. Use pointer
141 accesses. */
142 void test_vmem_derived_dtor_arryaccess ()
143 {
144 D5 d6;
145 sink (&d6);
146 }
147
148
149 struct D6: virtual B, virtual C3 // { dg-bogus "\\\[-Warray-bounds" }
150 {
151 ~D6 ()
152 {
153 int *p = a;
154 *p++ = __LINE__;
155 *p++ = __LINE__;
156 *p++ = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
157 }
158 };
159
160 /* Verify that only out of bounds accesses to members of an ordinary base
161 class made in the dtor of a derived class are diagnosed. Use pointer
162 accesses. */
163 void test_vmem_derived_dtor_ptraccess ()
164 {
165 D6 d7;
166 sink (&d7);
167 }