1 /* PR tree-optimization/83776: missing -Warray-bounds indexing past the end
3 Test to exercise warnings for computations of otherwise in-bounds indices
4 into strings that temporarily exceed the bounds of the string.
6 { dg-options "-O2 -Warray-bounds=2 -ftrack-macro-expansion=0" } */
13 typedef __WCHAR_TYPE__
wchar_t;
17 #define T(expr) sink (0, expr)
19 void test_narrow (void)
23 const char *p0
= "12";
24 const char *p1
= p0
+ i
; /* points at '2' or beyond */
25 const char *p2
= p1
+ i
; /* points at '\0' or beyond */
26 const char *p3
= p2
+ i
; /* points just past the end */
27 const char *p4
= p3
+ i
; /* invalid */
29 T (p0
[-1]); /* { dg-warning "array subscript \(-1|\[0-9\]+) is outside array bounds of .char\\\[3]." } */
33 T (p0
[3]); /* { dg-warning "array subscript 3 is outside array bounds of .char\\\[3]." } */
35 T (&p0
[-1]); /* { dg-warning "array subscript \(-1|\[0-9\]+) is \(above|below|outside\) array bounds of .char\\\[3]." } */
40 T (&p0
[4]); /* { dg-warning "array subscript 4 is \(above|outside\) array bounds of .char\\\[3]." } */
42 T (p1
[-3]); /* { dg-warning "array subscript \\\[-2, -1] is outside array bounds of .char\\\[3]." } */
47 T (p1
[ 2]); /* { dg-warning "array subscript 3 is outside array bounds of .char\\\[3]." } */
48 T (p1
[ 3]); /* { dg-warning "array subscript \\\[4, 5] is outside array bounds of .char\\\[3]." } */
50 T (&p1
[-3]); /* { dg-warning "array subscript \\\[-2, -1] is outside array bounds of .char\\\[3]." "bug" { xfail *-*-* } } */
56 T (&p1
[ 3]); /* { dg-warning "array subscript \\\[4, 6] is outside array bounds of .char\\\[3]." "bug" { xfail *-*-* } } */
58 T (p2
[-4]); /* { dg-warning "subscript \\\[-2, -1\\\] is outside array bounds of .char\\\[3]." } */
64 /* Even though the lower bound of p3's offsets is in bounds, in order
65 to subtract 4 from p3 and get a dereferenceable pointer its value
66 would have to be out-of-bounds. */
67 T (p3
[-4]); /* { dg-warning "array subscript -1 is outside array bounds of .char\\\[3]." } */
71 T (p3
[ 0]); /* { dg-warning "array subscript 3 is outside array bounds of .char\\\[3]." } */
73 T (p4
[-4]); /* { dg-warning "intermediate array offset 4 is outside array bounds of .char\\\[3]." } */
74 T (p4
[-3]); /* { dg-warning "intermediate array offset 4 is outside array bounds of .char\\\[3]." } */
75 T (p4
[-2]); /* { dg-warning "intermediate array offset 4 is outside array bounds of .char\\\[3]." } */
77 /* The final subscripts below are invalid. */
78 T (p4
[-1]); /* { dg-warning "array subscript 3 is outside array bounds of .char\\\[3]." } */
79 T (p4
[ 0]); /* { dg-warning "array subscript \\\[4, 5] is outside array bounds of .char\\\[3]." } */
83 void test_narrow_vflow (void)
85 int i
= SR (DIFF_MAX
- 2, DIFF_MAX
);
86 int j
= SR (1, DIFF_MAX
);
88 const char *p0
= "123";
89 const char *p1
= p0
+ i
; /* points at '2' or beyond */
90 const char *p2
= p1
+ i
; /* points at '\0' or beyond */
91 const char *p3
= p2
+ i
; /* points just past the end */
92 const char *p4
= p3
+ i
; /* invalid */
100 const wchar_t *p0
= L
"123";
101 const wchar_t *p1
= p0
+ i
; /* points at L'2' or beyond */
102 const wchar_t *p2
= p1
+ i
; /* points at L'3' or beyond */
103 const wchar_t *p3
= p2
+ i
; /* points at L'\0' or beyond */
104 const wchar_t *p4
= p3
+ i
; /* points just past the end */
105 const wchar_t *p5
= p4
+ i
; /* invalid */
111 T (p0
[4]); /* { dg-warning "array subscript 4 is outside array bounds of .\[a-z \]+\\\[4]." } */
117 T (p1
[ 3]); /* { dg-warning "array subscript 4 is outside array bounds of .\[a-z \]+\\\[4]." } */
124 T (&p1
[ 4]); /* { dg-warning "array subscript \\\[5, 6] is outside array bounds of .\[a-z \]+\\\[4]." "bug" { xfail *-*-* } } */
126 T (p2
[-5]); /* { dg-warning "array subscript \\\[-3, -1] is outside array bounds of .\[a-z \]+\\\[4]." } */
133 /* Even though the lower bound of p3's offsets is in bounds, in order
134 to subtract 5 from p3 and get a dereferenceable pointer its value
135 would have to be out-of-bounds. */
136 T (p3
[-5]); /* { dg-warning "array subscript \\\[-2, -1\\\] is outside array bounds of .\[a-z \]+\\\[4]." } */
142 T (p3
[ 1]); /* { dg-warning "array subscript 4 is outside array bounds of .\[a-z \]+\\\[4]." } */
144 T (p4
[-5]); /* { dg-warning "array subscript -1 is outside array bounds of .\[a-z \]+\\\[4]." } */
149 T (p4
[ 0]); /* { dg-warning "array subscript 4 is outside array bounds of .\[a-z \]+\\\[4]." } */