Debian Codegraph @

gcc/testsuite/gcc.dg/Warray-bounds-66.c

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