1 /* PR tree-optimization/83776: missing -Warray-bounds indexing past the end
3 Test to exercise detection of out-of-bounds indices into narrow string
6 { dg-options "-O2 -Warray-bounds -ftrack-macro-expansion=0" } */
17 #define S9 "123456789"
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]" "bug 86611" { xfail lp64 } } */
27 T (S1
[-1]); /* { dg-warning "array subscript -1 is below array bounds of .char\\\[2]" } */
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]" } */
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 lp64" { xfail lp64 } } */
41 T (S9
[-1]); /* { dg-warning "array subscript -1 is below array bounds of .char\\\[10]" } */
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]" } */
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]." } */
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)
72 T (p
[MIN
+ 1]); /* { dg-warning "array subscript -\[0-9\]+ is outside array bounds of .char\\\[8]." "bug 86611" { xfail lp64 } } */
73 T (p
[-1]); /* { dg-warning "array subscript -1 is outside array bounds of .char\\\[8]." } */
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]." } */
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 86611" { xfail lp64 } } */
91 T (q
[-5]); /* { dg-warning "array subscript -1 is outside array bounds of .char\\\[9]." } */
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 86611" { xfail lp64 } } */
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]." } */
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
)
120 T (S3
[SR (MIN
, -1)]); /* { dg-warning "array subscript -1 is below array bounds of .char\\\[4]" } */
122 T (S3
[SR (-2, -1)]); /* { dg-warning "array subscript -1 is below array bounds of .char\\\[4]" } */
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]" } */
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]" } */
141 T (S9
[SR (-2, -1)]); /* { dg-warning "array subscript -1 is below array bounds of .char\\\[10]" } */
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
)
161 p
= S1
+ SR (-999, 999);
177 T (*p
); /* { dg-warning "array subscript 2 is outside array bounds of .char\\\[2]." } */
180 T (*p
); /* { dg-warning "array subscript \\\[9, 99] is outside array bounds of .char\\\[2]." } */
182 p
= S8
+ SR (-999, 999);
185 p
= S8
+ SR (-9, -1);
186 T (*p
); /* { dg-warning "array subscript \\\[-9, -1] is outside array bounds of .char\\\[9]." } */
194 p
= S8
+ SR (-9, 123);
197 p
= S8
+ SR (8, 123);
200 p
= S8
+ SR (9, 123);
201 T (*p
); /* { dg-warning "array subscript 9 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
;
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
;
229 T (*p4
); /* { dg-warning "array subscript 4 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)
240 T (p
[SR (-9, -1)]); /* { dg-warning "array subscript \\\[-9, -1] is outside array bounds of .char\\\[8]." } */
244 T (p
[SR (8, 9)]); /* { dg-warning "array subscript 8 is outside array bounds of .char\\\[8]." } */
247 T (p
[5]); /* { dg-warning "array subscript \\\[9, 11] is outside array bounds of .char\\\[8]." } */