Daily bump.
[official-gcc.git] / gcc / testsuite / gcc.dg / Warray-bounds-32.c
blob02dac6522114a9b33ef0e0089ae06442e873da4a
1 /* PR tree-optimization/83776: missing -Warray-bounds indexing past the end
2 of a string literal
3 Test to exercise indices into wide string literals.
4 { dg-do compile }
5 { dg-options "-O2 -Warray-bounds -ftrack-macro-expansion=0" } */
7 #include "range.h"
9 #define MAX DIFF_MAX
10 #define MIN DIFF_MIN
12 typedef __WCHAR_TYPE__ wchar_t;
14 #define W2 L"12"
15 #define W3 L"123"
16 #define W4 L"1234"
17 #define W7 L"1234567"
18 #define W8 L"12345678"
19 #define W9 L"123456789"
21 void sink (int);
23 #define T(expr) sink (expr)
26 void wide_direct_cst (void)
28 T (W9[MIN]); /* { dg-warning "array subscript -\[0-9\]+ is below array bounds of .\[a-z \]+\\\[10]" "bug 86611" { xfail ilp32 } } */
29 T (W9[-1]); /* { dg-warning "array subscript -1 is below array bounds of .\[a-z \]+\\\[10]" } */
30 T (W9[11]); /* { dg-warning "array subscript 11 is above array bounds of .\[a-z \]+\\\[10]" } */
31 T (W9[MAX]); /* { dg-warning "array subscript \[0-9\]+ is above array bounds of .\[a-z \]+\\\[10]" } */
34 void wide_ptr_deref_cst (void)
36 const wchar_t *p = W8 + 9;
37 T (*p); /* { dg-warning "array subscript 9 is outside array bounds of .\[a-z \]+\\\[9]." } */
38 T (p[1]); /* { dg-warning "array subscript 10 is outside array bounds of .\[a-z \]+\\\[9]." } */
39 T (p[99]); /* { dg-warning "array subscript 108 is outside array bounds of .\[a-z \]+\\\[9]." } */
42 void wide_ptr_index_cst (void)
44 const wchar_t *p = W7;
46 T (p[1]);
47 T (p[8]); /* { dg-warning "array subscript 8 is outside array bounds of .\[a-z \]+\\\[8]." } */
48 T (p[99]); /* { dg-warning "array subscript 99 is outside array bounds of .\[a-z \]+\\\[8]." } */
49 T (p[MAX]); /* { dg-warning "array subscript -?\[0-9\]+ is outside array bounds of .\[a-z \]+\\\[8]." } */
53 void wide_direct_range (ptrdiff_t i, size_t j)
55 T (W9[i]);
56 T (W9[j]);
58 T (W9[SR (MIN, -9)]); /* { dg-warning "array subscript -9 is below array bounds of .\[a-z \]+\\\[10]" } */
59 T (W9[SR (MIN, -1)]); /* { dg-warning "array subscript -1 is below array bounds of .\[a-z \]+\\\[10]" } */
60 T (W9[SR (MIN, 0)]);
61 T (W9[SR (-2, -1)]); /* { dg-warning "array subscript -1 is below array bounds of .\[a-z \]+\\\[10]" } */
62 T (W9[SR (1, 2)]);
63 T (W9[SR (1, 9)]);
64 T (W9[SR (1, 999)]);
65 T (W9[SR (9, 999)]);
66 T (W9[SR (10, 999)]); /* { dg-warning "array subscript 10 is above array bounds of .\[a-z \]+\\\[10]" } */
67 T (W9[SR (99, MAX)]); /* { dg-warning "array subscript 99 is above array bounds of .\[a-z \]+\\\[10]" } */
70 void wide_ptr_deref_range (ptrdiff_t i, size_t j)
72 const wchar_t *p;
74 p = W8 + i;
75 T (*p);
77 p = W8 + j;
78 T (*p);
80 p = W8 + SR (-9, -1);
81 T (*p); /* { dg-warning "array subscript \\\[-9, -1] is outside array bounds of .\[a-z \]+\\\[9]." } */
83 p = W8 + SR (-9, 0);
84 T (*p);
86 p = W8 + SR (-9, 9);
87 T (*p);
89 p = W8 + SR (9, 123);
90 T (*p); /* { dg-warning "array subscript 9 is outside array bounds of .\[a-z \]+\\\[9]." } */
93 void wide_ptr_index_range (void)
95 const wchar_t *p;
97 p = W7;
98 T (p[SR (-9, -1)]); /* { dg-warning "array subscript \\\[-9, -1] is outside array bounds of .\[a-z \]+\\\[8]." } */
99 T (p[SR (-8, 0)]);
100 T (p[SR (0, MAX)]);
101 T (p[SR (1, 9)]);
102 T (p[SR (8, 9)]); /* { dg-warning "array subscript 8 is outside array bounds of .\[a-z \]+\\\[8]." } */
104 p = W7 + SR (4, 6);
105 T (p[5]); /* { dg-warning "array subscript \\\[9, 11] is outside array bounds of .\[a-z \]+\\\[8]." } */
108 void wide_ptr_index_range_1 (void)
111 int i = SR (1, 2);
112 const wchar_t *p1 = W2 + i;
114 T (p1[0]);
117 int i = SR (1, 2);
118 const wchar_t *p1 = W2 + i;
120 T (p1[1]);
123 int i = SR (1, 2);
124 const wchar_t *p1 = W2 + i;
126 T (p1[2]); /* { dg-warning "array subscript 3 is outside array bounds of .\[a-z \]+\\\[3]." } */
130 void wide_ptr_index_range_chain (void)
132 int i = SR (1, 2);
134 const wchar_t *p1 = W2 + i;
135 const wchar_t *p2 = p1 + i;
136 const wchar_t *p3 = p2 + i;
138 T (p1[-3]); /* { dg-warning "array subscript \\\[-2, -1] is outside array bounds of .\[a-z \]+\\\[3]." } */
139 T (p1[-2]);
140 T (p1[-1]);
141 T (p1[0]);
142 T (p1[1]);
143 T (p1[2]); /* { dg-warning "array subscript 3 is outside array bounds of .\[a-z \]+\\\[3]." } */
145 T (p2[-5]); /* { dg-warning "array subscript \\\[-3, -2] is outside array bounds of .\[a-z \]+\\\[3]." } */
146 T (p2[-4]); /* { dg-warning "array subscript \\\[-2, -1] is outside array bounds of .\[a-z \]+\\\[3]." } */
147 T (p2[-1]);
148 T (p2[0]);
149 T (p2[1]); /* { dg-warning "array subscript 3 is outside array bounds of .\[a-z \]+\\\[3]." } */
151 T (p3[0]); /* { dg-warning "array subscript 3 is outside array bounds of .\[a-z \]+\\\[3]." } */
152 T (p3[1]); /* { dg-warning "array subscript 4 is outside array bounds of .\[a-z \]+\\\[3]." } */
153 T (p3[9999]); /* { dg-warning "array subscript 10002 is outside array bounds of .\[a-z \]+\\\[3]." "" { target size20plus} } */
154 /* { dg-warning "array subscript \\\[-6382, -6379] is outside array bounds of .\[a-z \]+\\\[3]." "" { target { ! size20plus } } .-1 } */
155 /* Large offsets are indistinguishable from negative values. */
156 T (p3[DIFF_MAX]); /* { dg-warning "array subscript" "bug" { xfail *-*-* } } */
160 const wchar_t *p1 = W3 + i;
161 const wchar_t *p2 = p1 + i;
162 const wchar_t *p3 = p2 + i;
163 const wchar_t *p4 = p3 + i;
165 T (p1[-3]); /* { dg-warning "array subscript \\\[-2, -1] is outside array bounds of .\[a-z \]+\\\[4]." } */
166 T (p1[-2]);
167 T (p1[1]);
168 T (p1[2]);
169 T (p1[3]); /* { dg-warning "array subscript 4 is outside array bounds of .\[a-z \]+\\\[4]." } */
171 T (p3[1]); /* { dg-warning "array subscript 4 is outside array bounds of .\[a-z \]+\\\[4]." } */
175 void wide_ptr_index_range_4 (void)
177 int i = SR (1, 2);
178 const wchar_t *p1 = W4 + i;
179 const wchar_t *p2 = p1 + i;
180 const wchar_t *p3 = p2 + i;
181 const wchar_t *p4 = p3 + i;
183 T (p4[1]); /* { dg-warning "array subscript 5 is outside array bounds of .\[a-z \]+\\\[5]." } */