PR tree-optimization/84047 - missing -Warray-bounds on an out-of-bounds index into...
[official-gcc.git] / gcc / testsuite / gcc.dg / Warray-bounds-31.c
blobe0be1e598edc4b80dc89d5d210aa50ee32d77c49
1 /* PR tree-optimization/83776: missing -Warray-bounds indexing past the end
2 of a string literal
3 Test to exercise detection of out-of-bounds indices into narrow string
4 literals.
5 { dg-do compile }
6 { dg-options "-O2 -Warray-bounds -ftrack-macro-expansion=0" } */
8 #include "range.h"
10 #define MAX DIFF_MAX
11 #define MIN DIFF_MIN
13 #define S1 "1"
14 #define S3 "123"
15 #define S7 "1234567"
16 #define S8 "12345678"
17 #define S9 "123456789"
19 void sink (int, ...);
21 #define T(expr) sink (0, expr)
24 void narrow_direct_cst (void)
26 T (S1[MIN]); /* { dg-warning "array subscript -\[0-9\]+ is below array bounds of .char\\\[2]" "" { xfail *-*-* } } */
27 T (S1[-1]); /* { dg-warning "array subscript -1 is below array bounds of .char\\\[2]" } */
28 T (S1[0]);
29 T (S1[1]);
30 T (S1[2]); /* { dg-warning "array subscript 2 is above array bounds of .char\\\[2]" } */
31 T (S1[MAX]); /* { dg-warning "array subscript \[0-9\]+ is above array bounds of .char\\\[2]" } */
33 T (&S1[MIN]); /* { dg-warning "array subscript -\[0-9\]+ is below array bounds of .char\\\[2]" } */
34 T (&S1[-1]); /* { dg-warning "array subscript -1 is below array bounds of .char\\\[2]" } */
35 T (&S1[0]);
36 T (&S1[2]);
37 T (&S1[3]); /* { dg-warning "array subscript 3 is above array bounds of .char\\\[2]" } */
38 T (&S1[MAX]); /* { dg-warning "array subscript \[0-9\]+ is above array bounds of .char\\\[2]" } */
40 T (S9[MIN]); /* { dg-warning "array subscript -\[0-9\]+ is below array bounds of .char\\\[10]" "" { xfail *-*-* } } */
41 T (S9[-1]); /* { dg-warning "array subscript -1 is below array bounds of .char\\\[10]" } */
42 T (S9[9]);
43 T (S9[10]); /* { dg-warning "array subscript 10 is above array bounds of .char\\\[10]" } */
44 T (S9[11]); /* { dg-warning "array subscript 11 is above array bounds of .char\\\[10]" } */
45 T (S9[MAX]); /* { dg-warning "array subscript \[0-9\]+ is above array bounds of .char\\\[10]" } */
47 T (&S9[MIN]); /* { dg-warning "array subscript -\[0-9\]+ is below array bounds of .char\\\[10]" } */
48 T (&S9[-1]); /* { dg-warning "array subscript -1 is below array bounds of .char\\\[10]" } */
49 T (&S9[9]);
50 T (&S9[10]);
51 T (&S9[11]); /* { dg-warning "array subscript 11 is above array bounds of .char\\\[10]" } */
52 T (&S9[MAX]); /* { dg-warning "array subscript \[0-9\]+ is above array bounds of .char\\\[10]" } */
55 void narrow_ptr_deref_cst (void)
57 const char *p = S8 + 9;
59 T (*(p + MIN)); /* { dg-warning "array subscript -\[0-9\]+ is outside array bounds of .char\\\[9]." } */
60 T (*(p - 10)); /* { dg-warning "array subscript -1 is outside array bounds of .char\\\[9]." } */
61 T (*(p - 9));
62 T (*(p - 1));
63 T (*p); /* { dg-warning "array subscript 9 is outside array bounds of .char\\\[9]." } */
64 T (*(p + 1)); /* { dg-warning "array subscript 10 is outside array bounds of .char\\\[9]." } */
65 T (*(p + 2)); /* { dg-warning "array subscript 11 is outside array bounds of .char\\\[9]." } */
68 void narrow_ptr_index_cst (void)
70 const char *p = S7;
72 T (p[MIN + 1]); /* { dg-warning "array subscript -\[0-9\]+ is outside array bounds of .char\\\[8]." "bug 84047" { xfail *-*-* } } */
73 T (p[-1]); /* { dg-warning "array subscript -1 is outside array bounds of .char\\\[8]." } */
74 T (p[0]);
75 T (p[1]);
76 T (p[8]); /* { dg-warning "array subscript 8 is outside array bounds of .char\\\[8]." } */
77 T (p[99]); /* { dg-warning "array subscript 99 is outside array bounds of .char\\\[8]." } */
78 T (p[MAX]); /* { dg-warning "array subscript \[0-9\]+ is outside array bounds of .char\\\[8]." } */
80 T (&p[MIN + 1]); /* { dg-warning "array subscript -\[0-9\]+ is \(below|outside\) array bounds of .char\\\[8]." } */
81 T (&p[-1]); /* { dg-warning "array subscript -1 is \(below|outside\) array bounds of .char\\\[8]." } */
82 T (&p[0]);
83 T (&p[1]);
84 T (&p[8]);
85 T (&p[9]); /* { dg-warning "array subscript 9 is \(above|outside\) array bounds of .char\\\[8]." } */
86 T (&p[99]); /* { dg-warning "array subscript 99 is \(above|outside\) array bounds of .char\\\[8]." } */
87 T (&p[MAX]); /* { dg-warning "array subscript \[0-9\]+ is \(above|outside\) array bounds of .char\\\[8]." } */
89 const char *q = S8 + 4;
90 T (q[MIN + 1]); /* { dg-warning "array subscript -?\[0-9\]+ is outside array bounds of .char\\\[9]." "bug 84047" { xfail *-*-* } } */
91 T (q[-5]); /* { dg-warning "array subscript -1 is outside array bounds of .char\\\[9]." } */
92 T (q[-4]);
93 T (q[0]);
94 T (q[1]);
95 T (q[3]);
96 T (q[4]);
97 T (q[5]); /* { dg-warning "array subscript 9 is outside array bounds of .char\\\[9]." } */
98 T (q[99]); /* { dg-warning "array subscript 103 is outside array bounds of .char\\\[9]." } */
99 T (q[MAX - 4]); /* { dg-warning "array subscript \[0-9\]+ is outside array bounds of .char\\\[9]." } */
100 T (q[MAX - 3]); /* { dg-warning "array subscript \[0-9\]+ is outside array bounds of .char\\\[9]." "bug 84047" { xfail *-*-* } } */
102 T (&q[MIN + 1]); /* { dg-warning "array subscript -?\[0-9\]+ is \(below|outside\) array bounds of .char\\\[9]." } */
103 T (&q[-5]); /* { dg-warning "array subscript -1 is \(below|outside\) array bounds of .char\\\[9]." } */
104 T (&q[-4]);
105 T (&q[0]);
106 T (&q[1]);
107 T (&q[5]);
108 T (&q[6]); /* { dg-warning "array subscript 10 is \(above|outside\) array bounds of .char\\\[9]." } */
109 T (&q[99]); /* { dg-warning "array subscript 103 is \(above|outside\) array bounds of .char\\\[9]." } */
110 T (&q[MAX - 4]); /* { dg-warning "array subscript \[0-9\]+ is \(above|outside\) array bounds of .char\\\[9]." } */
111 T (&q[MAX - 3]); /* { dg-warning "array subscript -?\[0-9\]+ is \(below|outside\) array bounds of .char\\\[9]." } */
115 void narrow_direct_range (ptrdiff_t i, size_t j)
117 T (S3[i]);
118 T (S9[j]);
120 T (S3[SR (MIN, -1)]); /* { dg-warning "array subscript -1 is below array bounds of .char\\\[4]" } */
121 T (S3[SR (MIN, 0)]);
122 T (S3[SR (-2, -1)]); /* { dg-warning "array subscript -1 is below array bounds of .char\\\[4]" } */
123 T (S3[SR (1, 2)]);
124 T (S3[SR (1, 999)]);
125 T (S3[SR (2, 999)]);
126 T (S3[SR (3, 999)]);
127 T (S3[SR (4, 999)]); /* { dg-warning "array subscript 4 is above array bounds of .char\\\[4]" } */
129 T (&S3[SR (MIN, -1)]); /* { dg-warning "array subscript -1 is below array bounds of .char\\\[4]" } */
130 T (&S3[SR (MIN, 0)]);
131 T (&S3[SR (-2, -1)]); /* { dg-warning "array subscript -1 is below array bounds of .char\\\[4]" } */
132 T (&S3[SR (1, 2)]);
133 T (&S3[SR (1, 999)]);
134 T (&S3[SR (2, 999)]);
135 T (&S3[SR (4, 999)]);
136 T (&S3[SR (5, 999)]); /* { dg-warning "array subscript 5 is above array bounds of .char\\\[4]" } */
138 T (S9[SR (MIN, -9)]); /* { dg-warning "array subscript -9 is below array bounds of .char\\\[10]" } */
139 T (S9[SR (MIN, -1)]); /* { dg-warning "array subscript -1 is below array bounds of .char\\\[10]" } */
140 T (S9[SR (MIN, 0)]);
141 T (S9[SR (-2, -1)]); /* { dg-warning "array subscript -1 is below array bounds of .char\\\[10]" } */
142 T (S9[SR (1, 2)]);
143 T (S9[SR (1, 9)]);
144 T (S9[SR (1, 999)]);
145 T (S9[SR (9, 999)]);
146 T (S9[SR (10, 999)]); /* { dg-warning "array subscript 10 is above array bounds of .char\\\[10]" } */
147 T (S9[SR (99, MAX)]); /* { dg-warning "array subscript 99 is above array bounds of .char\\\[10]" } */
151 void narrow_ptr_deref_range (ptrdiff_t i, size_t j)
153 const char *p;
155 p = S1 + i;
156 T (*p);
158 p = S1 + j;
159 T (*p);
161 p = S1 + SR (-999, 999);
162 T (*p);
164 p = S1 + SR (-1, 1);
165 T (*p);
167 p = S1 + SR (-1, 0);
168 T (*p);
170 p = S1 + SR (0, 1);
171 T (*p);
173 p = S1 + SR (1, 2);
174 T (*p);
176 p = S1 + SR (2, 3);
177 T (*p); /* { dg-warning "array subscript \\\[2, 3] is outside array bounds of .char\\\[2]." } */
179 p = S1 + SR (9, 99);
180 T (*p); /* { dg-warning "array subscript \\\[9, 99] is outside array bounds of .char\\\[2]." } */
182 p = S8 + SR (-999, 999);
183 T (*p);
185 p = S8 + SR (-9, -1);
186 T (*p); /* { dg-warning "array subscript \\\[-9, -1] is outside array bounds of .char\\\[9]." } */
188 p = S8 + SR (-9, 0);
189 T (*p);
191 p = S8 + SR (-9, 9);
192 T (*p);
194 p = S8 + SR (-9, 123);
195 T (*p);
197 p = S8 + SR (8, 123);
198 T (*p);
200 p = S8 + SR (9, 123);
201 T (*p); /* { dg-warning "array subscript \\\[9, 123] is outside array bounds of .char\\\[9]." } */
204 const char *p1 = S3 + i;
205 const char *p2 = p1 + i;
206 const char *p3 = p2 + i;
207 const char *p4 = p3 + i;
208 const char *p5 = p4 + i;
210 T (*p1);
211 T (*p2);
212 T (*p3);
213 T (*p4);
214 T (*p5);
218 i = SR (1, 2);
220 const char *p1 = S3 + SR (1, DIFF_MAX - 1);
221 const char *p2 = p1 + i;
222 const char *p3 = p2 + i;
223 const char *p4 = p3 + i;
224 const char *p5 = p4 + i;
226 T (*p1);
227 T (*p2);
228 T (*p3);
229 T (*p4); /* { dg-warning "array subscript \\\[4, \[0-9\]+] is outside array bounds of .char\\\[4]." } */
230 T (*p5); /* { dg-warning "array subscript \\\[5, \[0-9\]+] is outside array bounds of .char\\\[4]." } */
235 void narrow_ptr_index_range (void)
237 const char *p;
239 p = S7;
240 T (p[SR (-9, -1)]); /* { dg-warning "array subscript \\\[-9, -1] is outside array bounds of .char\\\[8]." } */
241 T (p[SR (-8, 0)]);
242 T (p[SR (0, MAX)]);
243 T (p[SR (1, 9)]);
244 T (p[SR (8, 9)]); /* { dg-warning "array subscript \\\[8, 9] is outside array bounds of .char\\\[8]." } */
246 p = S7 + SR (4, 6);
247 T (p[5]); /* { dg-warning "array subscript \\\[9, 11] is outside array bounds of .char\\\[8]." } */