]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/testsuite/gcc.dg/Warray-bounds-66.c
Correct handling of variable offset minus constant in -Warray-bounds [PR100137]
[thirdparty/gcc.git] / gcc / testsuite / gcc.dg / Warray-bounds-66.c
CommitLineData
3f9a497d
MS
1/* PR middle-end/82608 - missing -Warray-bounds on an out-of-bounds VLA index
2 { dg-do compile }
28d3b78d
TV
3 { dg-options "-O2 -Wall -Wno-uninitialized -ftrack-macro-expansion=0" }
4 { dg-require-effective-target alloca } */
3f9a497d
MS
5
6#include "range.h"
7
8typedef __INT16_TYPE__ int16_t;
9
10#define alloca(n) __builtin_alloca (n)
11
12void* calloc (size_t, size_t);
13void* malloc (size_t);
14
15void sink (void*, ...);
16#define sink(...) sink (0, __VA_ARGS__)
17
18#define T(x) (sink (x))
19
20__attribute__ ((alloc_size (1))) void* alloc (size_t);
21
22
23void test_alloca_cst (void)
24{
25 {
26 char *p = alloca (1);
27 sink (p);
28 T (p[0]);
29 T (p[1]); // { dg-warning "subscript 1 is outside array bounds of 'char\\\[1\\\]'" }
30 }
31
32 {
33 char *p = alloca (2);
34 sink (p);
35 T (p[0]), T (p[1]);
36 T (p[2]); // { dg-warning "subscript 2 is outside array bounds of 'char\\\[2\\\]'" }
37 }
38
39 {
40 char *p = alloca (3);
41 sink (p);
42 T (p[0]), T (p[1]), T (p[2]);
43 T (p[3]); // { dg-warning "subscript 3 is outside array bounds of 'char\\\[3\\\]'" }
44 }
45}
46
47
48void test_alloca_char_range (int i, unsigned n, size_t sz)
49{
50 {
51 // Be sure to exercise signed as well as unsigned arguments.
52 char *p = alloca (i);
53 sink (p);
54 T (p[0]), T (p[1]), T (p[12345]);
55 T (p[-1]); // { dg-warning "subscript -1 is outside array bounds of 'char\\\[" }
56 }
57
58 {
59 char *p = alloca (n);
60 sink (p);
61 T (p[0]), T (p[1]), T (p[12345]);
62 T (p[-1]); // { dg-warning "subscript -1 is outside array bounds of 'char\\\[" }
63 }
64
65 {
66 char *p = alloca (sz);
67 sink (p);
68 T (p[0]), T (p[1]), T (p[23456]);
69 T (p[-1]); // { dg-warning "subscript -1 is outside array bounds of 'char\\\[" }
70 }
71
72 {
73 char *p = alloca (UR (0, 1));
74 sink (p);
75 T (p[0]);
76 T (p[1]); // { dg-warning "subscript 1 is outside array bounds of 'char\\\[1\\\]'" }
77 }
78
79 {
80 char *p = alloca (UR (0, 2));
81 sink (p);
82 sink (p[0], p[1]);
83 sink (p[2]); // { dg-warning "subscript 2 is outside array bounds of 'char\\\[2\\\]'" }
84 }
85
86 {
87 char *p = alloca (UR (0, 3));
88 sink (p);
89 T (p[0]), T (p[1]), T (p[2]);
90 T (p[3]); // { dg-warning "subscript 3 is outside array bounds of 'char\\\[3\\\]'" }
91 }
92
93 {
94 char *p = alloca (UR (1, 3));
95 sink (p);
96 T (p[0]), T (p[1]), T (p[2]);
97 T (p[3]); // { dg-warning "subscript 3 is outside array bounds of 'char\\\[3\\\]'" }
98 }
99
100 {
101 char *p = alloca (UR (2, 3));
102 sink (p);
103 T (p[0]), T (p[1]), T (p[2]);
104 T (p[3]); // { dg-warning "subscript 3 is outside array bounds of 'char\\\[3\\\]'" }
105 }
106}
107
108
109void test_alloca_int16_range (unsigned n)
110{
111 int16_t *p;
112 {
113 p = alloca (n); // { dg-message "allocated by " }
114 sink (p);
115 T (p[0]), T (p[1]), T (p[12345]);
116 T (p[-1]); // { dg-warning "subscript -1 is outside array bounds of 'int16_t\\\[" }
117 }
118
119 {
a1108556 120 p = alloca (UR (0, 1)); // { dg-message "at offset \\d+ into object of size \\\[0, 1] allocated by '__builtin_alloca'" "note" }
3f9a497d
MS
121 sink (p);
122 T (p[0]); // { dg-warning "subscript 'int16_t {aka short int}\\\[0\\\]' is partly outside array bounds of 'unsigned char\\\[1]'" }
123 T (p[1]); // { dg-warning "subscript 1 is outside array bounds of 'int16_t\\\[0]'" }
124 }
125
126 {
a1108556 127 p = alloca (UR (0, 2)); // { dg-message "at offset \\d+ into object of size \\\[0, 2] allocated by '__builtin_alloca'" "note" }
3f9a497d
MS
128 sink (p);
129 sink (p[0]);
130 sink (p[1]); // { dg-warning "subscript 1 is outside array bounds of 'int16_t\\\[1]'" }
131 sink (p[2]); // { dg-warning "subscript 2 is outside array bounds of 'int16_t\\\[1\\\]'" }
132 }
133
134 {
a1108556 135 p = alloca (UR (0, 3)); // { dg-message "at offset \\d+ into object of size \\\[0, 3] allocated by '__builtin_alloca'" "note" }
3f9a497d
MS
136 sink (p);
137 T (p[0]);
138 T (p[1]); // { dg-warning "subscript 'int16_t {aka short int}\\\[1\\\]' is partly outside array bounds of 'unsigned char\\\[3]'" }
139 T (p[2]); // { dg-warning "subscript 2 is outside array bounds of 'int16_t\\\[1\\\]'" }
140 T (p[3]); // { dg-warning "subscript 3 is outside array bounds of 'int16_t\\\[1\\\]'" }
141 }
142
143 {
a1108556 144 p = alloca (UR (1, 3)); // { dg-message "at offset 1|2|3 into object of size \\\[1, 3] allocated by '__builtin_alloca'" "note" }
3f9a497d
MS
145 sink (p);
146 T (p[0]);
147 T (p[1]); // { dg-warning "subscript 'int16_t {aka short int}\\\[1\\\]' is partly outside array bounds of 'unsigned char\\\[3]'" }
148 T (p[2]); // { dg-warning "subscript 2 is outside array bounds of 'int16_t\\\[1\\\]'" }
149 T (p[3]); // { dg-warning "subscript 3 is outside array bounds of 'int16_t\\\[1\\\]'" }
150 }
151
152 {
a1108556 153 p = alloca (UR (2, 3)); // { dg-message "at offset 2|4 into object of size \\\[2, 3] allocated by '__builtin_alloca'" "note" }
3f9a497d
MS
154 sink (p);
155 T (p[0]);
156 T (p[1]); // { dg-warning "subscript 'int16_t {aka short int}\\\[1\\\]' is partly outside array bounds of 'unsigned char\\\[3]'" }
157 T (p[2]); // { dg-warning "subscript 2 is outside array bounds of 'int16_t\\\[1\\\]'" }
158 T (p[3]); // { dg-warning "subscript 3 is outside array bounds of 'int16_t\\\[1\\\]'" }
159 }
160
161 {
a1108556 162 p = alloca (UR (3, 4)); // { dg-message "at offset 4|6 into object of size \\\[3, 4] allocated by '__builtin_alloca'" "note" }
3f9a497d
MS
163 sink (p);
164 T (p[0]);
165 T (p[1]);
166 T (p[2]); // { dg-warning "subscript 2 is outside array bounds of 'int16_t\\\[2\\\]'" }
167 T (p[3]); // { dg-warning "subscript 3 is outside array bounds of 'int16_t\\\[2\\\]'" }
168 }
169}
170
171
172void test_vla_cst (void)
173{
174 int n = 1;
175 {
176 char a[n];
177 sink (a);
178 T (a[0]);
179 T (a[1]); // { dg-warning "subscript 1 is (above|outside) array bounds " }
180 }
181
182 {
183 n = 2;
184 char a[n];
185 sink (a);
186 T (a[0]), T (a[1]);
187 T (a[2]); // { dg-warning "subscript 2 is (above|outside) array bounds " }
188 }
189
190 {
191 n = 3;
192 char a[n], *p = a;
193 sink (p);
194 T (p[0]), T (p[1]), T (p[2]);
195 T (p[3]); // { dg-warning "subscript 3 is (above|outside) array bounds " }
196 }
197}
198
199
200void test_vla_char_range (int i, unsigned n, size_t sz)
201{
202 {
203 char a[i];
204 sink (a);
205 T (a[0]), T (a[1]), T (a[12345]);
206 T (a[-1]); // { dg-warning "subscript -1 is (below|outside) array bounds of 'char\\\[" }
207 }
208
209 {
210 char a[n];
211 sink (a);
212 T (a[0]), T (a[1]), T (a[12345]);
213 T (a[-1]); // { dg-warning "subscript -1 is (below|outside) array bounds of 'char\\\[" }
214 }
215
216 {
217 char a[sz];
218 sink (a);
219 T (a[0]), T (a[1]), T (a[23456]);
220 T (a[-1]); // { dg-warning "subscript -1 is (below|outside) array bounds of 'char\\\[" }
221 }
222
223 {
224 char a[UR (0, 1)];
225 sink (a);
226 T (a[0]);
227 T (a[1]); // { dg-warning "subscript 1 is outside array bounds of 'char\\\[1\\\]'" "pr82608" { xfail *-*-* } }
228 }
229
230 {
231 char a[UR (0, 2)];
232 sink (a);
233 sink (a[0], a[1]);
234 sink (a[2]); // { dg-warning "subscript 2 is outside array bounds of 'char\\\[2\\\]'" "pr82608" { xfail *-*-* } }
235 }
236
237 {
238 char a[UR (0, 3)];
239 sink (a);
240 T (a[0]), T (a[1]), T (a[2]);
241 T (a[3]); // { dg-warning "subscript 3 is outside array bounds of 'char\\\[3\\\]'" "pr82608" { xfail *-*-* } }
242 }
243
244 {
245 char a[UR (1, 3)];
246 sink (a);
247 T (a[0]), T (a[1]), T (a[2]);
248 T (a[3]); // { dg-warning "subscript 3 is outside array bounds of 'char\\\[3\\\]'" "pr82608" { xfail *-*-* } }
249 }
250
251 {
252 char a[UR (2, 3)];
253 sink (a);
254 T (a[0]), T (a[1]), T (a[2]);
255 T (a[3]); // { dg-warning "subscript 3 is outside array bounds of 'char\\\[3\\\]'" "pr82608" { xfail *-*-* } }
256 }
257}