1 /* PR tree-optimization/84047 - missing -Warray-bounds on an out-of-bounds
4 { dg-options "-O2 -Warray-bounds=2 -ftrack-macro-expansion=0" }
5 { dg-skip-if "too many arguments in function call" { bpf-*-* } } */
14 #define T(...) sink (0, __VA_ARGS__)
16 void test_global_char_array (void)
21 T (p
[MIN
]); /* { dg-warning "subscript -\[0-9\]+ is outside array bounds of .char\\\[1]." } */
22 T (p
[-1]); /* { dg-warning "subscript -1 is outside array bounds of .char\\\[1]." } */
24 T (p
[1]); /* { dg-warning "subscript 1 is outside array bounds of .char\\\[1]." } */
25 T (p
[MAX
]); /* { dg-warning "subscript \[0-9\]+ is outside array bounds of .char\\\[1]." } */
27 T (&p
[MIN
]); /* { dg-warning "subscript -\[0-9\]+ is \(below|outside\) array bounds of .char\\\[1]." } */
28 T (&p
[-1]); /* { dg-warning "subscript -1 is \(below|outside\) array bounds of .char\\\[1]." } */
31 T (&p
[2]); /* { dg-warning "subscript 2 is \(above|outside\) array bounds of .char\\\[1]." } */
32 T (&p
[MAX
]); /* { dg-warning "subscript \[0-9\]+ is \(above|outside\) array bounds of .char\\\[1]." } */
37 T (q
[MIN
]); /* { dg-warning "subscript -\[0-9\]+ is outside array bounds of .char\\\[3]." } */
38 T (q
[-1]); /* { dg-warning "subscript -1 is outside array bounds of .char\\\[3]." } */
42 T (q
[3]); /* { dg-warning "subscript 3 is outside array bounds of .char\\\[3]." } */
43 T (q
[MAX
]); /* { dg-warning "subscript \[0-9\]+ is outside array bounds of .char\\\[3]." } */
45 T (&q
[MIN
]); /* { dg-warning "subscript -\[0-9\]+ is \(below|outside\) array bounds of .char\\\[3]." } */
46 T (&q
[-1]); /* { dg-warning "subscript -1 is \(below|outside\) array bounds of .char\\\[3]." } */
51 T (&q
[4]); /* { dg-warning "subscript 4 is \(above|outside\) array bounds of .char\\\[3]." } */
52 T (&q
[MAX
]); /* { dg-warning "subscript \[0-9\]+ is \(above|outside\) array bounds of .char\\\[3]." } */
56 void test_global_int_array (void)
58 /* Use smaller values to prevent false negatives due to undetected
59 integer overflow/wrapping. */
60 ptrdiff_t min
= MIN
/ sizeof (int);
61 ptrdiff_t max
= MAX
/ sizeof (int);
68 T (p
[min
]); /* { dg-warning "subscript -\[0-9\]+ is outside array bounds of .int\\\[1]." } */
69 T (p
[-1]); /* { dg-warning "subscript -1 is outside array bounds of .int\\\[1]." } */
71 T (p
[1]); /* { dg-warning "subscript 1 is outside array bounds of .int\\\[1]." } */
72 T (p
[max
]); /* { dg-warning "subscript \[0-9\]+ is outside array bounds of .int\\\[1]." } */
74 T (&p
[min
]); /* { dg-warning "subscript -\[0-9\]+ is \(below|outside\) array bounds of .int\\\[1]." } */
75 T (&p
[-1]); /* { dg-warning "subscript -1 is \(below|outside\) array bounds of .int\\\[1]." } */
77 T (&p
[2]); /* { dg-warning "subscript 2 is \(above|outside\) array bounds of .int\\\[1]." } */
78 T (&p
[max
]); /* { dg-warning "subscript \[0-9\]+ is \(above|outside\) array bounds of .int\\\[1]." } */
82 T (q
[min
]); /* { dg-warning "subscript -\[0-9\]+ is outside array bounds of .int\\\[3]." } */
83 T (q
[-1]); /* { dg-warning "subscript -1 is outside array bounds of .int\\\[3]." } */
87 T (q
[3]); /* { dg-warning "subscript 3 is outside array bounds of .int\\\[3]." } */
88 T (q
[max
]); /* { dg-warning "subscript \[0-9\]+ is outside array bounds of .int\\\[3]." } */
90 T (&q
[min
]); /* { dg-warning "subscript -\[0-9\]+ is \(below|outside\) array bounds of .int\\\[3]." } */
91 T (&q
[-1]); /* { dg-warning "subscript -1 is \(below|outside\) array bounds of .int\\\[3]." } */
96 T (&q
[4]); /* { dg-warning "subscript 4 is \(above|outside\) array bounds of .int\\\[3]." } */
97 T (&q
[max
]); /* { dg-warning "subscript \[0-9\]+ is \(above|outside\) array bounds of .int\\\[3]." } */
101 void test_global_short_2dim_array (void)
103 extern short giar3_5
[3][5];
105 short *p
= giar3_5
[0];
107 /* The access below isn't diagnosed because the reference is transformed
108 into MEM_REF (short*, &giar3_5, 0), i.e., *giar3_5[0][0]. */
109 T (p
[MIN
]); /* { dg-warning "subscript -\[0-9\]+ is outside array bounds of .short int\\\[3]" "bug" { xfail *-*-*} } */
110 T (p
[-1]); /* { dg-warning "subscript -1 is outside array bounds of .short int\\\[3]" } */
114 T (p
[15]); /* { dg-warning "subscript 15 is outside array bounds of .short int\\\[3]" } */
115 T (p
[MAX
]); /* { dg-warning "subscript -?\[0-9\]+ is outside array bounds of .short int\\\[3]" } */
117 T (&p
[MIN
]); /* { dg-warning "subscript -\[0-9\]+ is \(below|outside\) array bounds of .short int\\\[3]" "bug" { xfail *-*-* } } */
118 T (&p
[-1]); /* { dg-warning "subscript -1 is \(below|outside\) array bounds of .short int\\\[3]" } */
123 T (&p
[16]); /* { dg-warning "subscript 16 is \(above|outside\) array bounds of .short int\\\[3]" "pr??????" { xfail *-*-* } } */
124 T (&p
[MAX
]); /* { dg-warning "subscript -?\[0-9\]+ is \(above|outside\) array bounds of .short int\\\[3]" } */
128 void test_local_char_array (void)
133 T (p
[MIN
]); /* { dg-warning "subscript -\[0-9\]+ is outside array bounds of .char\\\[1]." } */
134 T (p
[-1]); /* { dg-warning "subscript -1 is outside array bounds of .char\\\[1]." } */
136 T (p
[1]); /* { dg-warning "subscript 1 is outside array bounds of .char\\\[1]." } */
137 T (p
[MAX
]); /* { dg-warning "subscript \[0-9\]+ is outside array bounds of .char\\\[1]." } */
139 T (&p
[MIN
]); /* { dg-warning "subscript -\[0-9\]+ is \(below|outside\) array bounds of .char\\\[1]." } */
140 T (&p
[-1]); /* { dg-warning "subscript -1 is \(below|outside\) array bounds of .char\\\[1]." } */
143 T (&p
[2]); /* { dg-warning "subscript 2 is \(above|outside\) array bounds of .char\\\[1]." } */
144 T (&p
[MAX
]); /* { dg-warning "subscript \[0-9\]+ is \(above|outside\) array bounds of .char\\\[1]." } */
146 char ar3
[3] = { 1, 2, 3 };
149 T (p
[MIN
]); /* { dg-warning "subscript -\[0-9\]+ is outside array bounds of .char\\\[3]." } */
150 T (p
[-1]); /* { dg-warning "subscript -1 is outside array bounds of .char\\\[3]." } */
154 T (p
[3]); /* { dg-warning "subscript 3 is outside array bounds of .char\\\[3]." } */
155 T (p
[MAX
]); /* { dg-warning "subscript \[0-9\]+ is outside array bounds of .char\\\[3]." } */
157 T (&p
[MIN
]); /* { dg-warning "subscript -\[0-9\]+ is \(below|outside\) array bounds of .char\\\[3]." } */
158 T (&p
[-1]); /* { dg-warning "subscript -1 is \(below|outside\) array bounds of .char\\\[3]." } */
163 T (&p
[4]); /* { dg-warning "subscript 4 is \(above|outside\) array bounds of .char\\\[3]." } */
164 T (&p
[MAX
]); /* { dg-warning "subscript \[0-9\]+ is \(above|outside\) array bounds of .char\\\[3]." } */
172 void test_struct_array_cst (void)
174 T (s
[0].a
[0] + s
[0].a
[1] + s
[0].b
[0] + s
[0].b
[1] + s
[0].b
[2] + s
[0].b
[2]
175 + s
[1].a
[0] + s
[1].a
[1] + s
[1].b
[0] + s
[1].b
[1] + s
[1].b
[2] + s
[1].b
[2]
176 + s
[2].a
[0] + s
[2].a
[1] + s
[2].b
[0] + s
[2].b
[1] + s
[2].b
[2] + s
[2].b
[2]
177 + s
[3].a
[0] + s
[3].a
[1] + s
[3].b
[0] + s
[3].b
[1] + s
[3].b
[2] + s
[3].b
[2]);
188 T (s
[0].a
[2]); /* { dg-warning "subscript 2 is above array bounds of .int\\\[2\\\]." } */
189 T (s
[0].b
[3]); /* { dg-warning "subscript 3 is above array bounds of .int\\\[3\\\]." } */
190 T (s
[1].a
[2]); /* { dg-warning "subscript 2 is above array bounds of .int\\\[2\\\]." } */
191 T (s
[1].b
[3]); /* { dg-warning "subscript 3 is above array bounds of .int\\\[3\\\]." } */
192 T (s
[2].a
[2]); /* { dg-warning "subscript 2 is above array bounds of .int\\\[2\\\]." } */
193 T (s
[2].b
[3]); /* { dg-warning "subscript 3 is above array bounds of .int\\\[3\\\]." } */
194 T (s
[3].a
[2]); /* { dg-warning "subscript 2 is above array bounds of .int\\\[2\\\]." } */
195 T (s
[3].b
[3]); /* { dg-warning "subscript 3 is above array bounds of .int\\\[3\\\]." } */
197 T (s
[4].a
[0]); /* { dg-warning "subscript 4 is above array bounds of .struct S\\\[4\\\]." } */
198 T (s
[4].a
[2]); /* { dg-warning "subscript 4 is above array bounds of .struct S\\\[4\\\]." } */
199 /* { dg-warning "subscript 2 is above array bounds of .int\\\[2\\\]." "" { target *-*-* } .-1 } */