Daily bump.
[official-gcc.git] / gcc / testsuite / gcc.dg / Warray-bounds-66.c
blob6ab3398c762d908556884bb9f7f2c94e9194eb2f
1 /* PR middle-end/82608 - missing -Warray-bounds on an out-of-bounds VLA index
2 { dg-do compile }
3 { dg-options "-O2 -Wall -Wno-uninitialized -ftrack-macro-expansion=0" }
4 { dg-require-effective-target alloca } */
6 #include "range.h"
8 typedef __INT16_TYPE__ int16_t;
10 #define alloca(n) __builtin_alloca (n)
12 void* calloc (size_t, size_t);
13 void* malloc (size_t);
15 void sink (void*, ...);
16 #define sink(...) sink (0, __VA_ARGS__)
18 #define T(x) (sink (x))
20 __attribute__ ((alloc_size (1))) void* alloc (size_t);
23 void test_alloca_cst (void)
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\\\]'" }
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\\\]'" }
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\\\]'" }
48 void test_alloca_char_range (int i, unsigned n, size_t sz)
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\\\[" }
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\\\[" }
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\\\[" }
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\\\]'" }
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\\\]'" }
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\\\]'" }
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\\\]'" }
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\\\]'" }
109 void test_alloca_int16_range (unsigned n)
111 int16_t *p;
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\\\[" }
120 p = alloca (UR (0, 1)); // { dg-message "at offset \\d+ into object of size \\\[0, 1] allocated by '__builtin_alloca'" "note" }
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]'" }
127 p = alloca (UR (0, 2)); // { dg-message "at offset \\d+ into object of size \\\[0, 2] allocated by '__builtin_alloca'" "note" }
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\\\]'" }
135 p = alloca (UR (0, 3)); // { dg-message "at offset \\d+ into object of size \\\[0, 3] allocated by '__builtin_alloca'" "note" }
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\\\]'" }
144 p = alloca (UR (1, 3)); // { dg-message "at offset 1|2|3 into object of size \\\[1, 3] allocated by '__builtin_alloca'" "note" }
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\\\]'" }
153 p = alloca (UR (2, 3)); // { dg-message "at offset 2|4 into object of size \\\[2, 3] allocated by '__builtin_alloca'" "note" }
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\\\]'" }
162 p = alloca (UR (3, 4)); // { dg-message "at offset 4|6 into object of size \\\[3, 4] allocated by '__builtin_alloca'" "note" }
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\\\]'" }
172 void test_vla_cst (void)
174 int n = 1;
176 char a[n];
177 sink (a);
178 T (a[0]);
179 T (a[1]); // { dg-warning "subscript 1 is (above|outside) array bounds " }
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 " }
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 " }
200 void test_vla_char_range (int i, unsigned n, size_t sz)
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\\\[" }
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\\\[" }
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\\\[" }
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 *-*-* } }
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 *-*-* } }
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 *-*-* } }
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 *-*-* } }
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 *-*-* } }