1 /* PR middle-end/84051 - missing -Warray-bounds on an out-of-bounds access
4 { dg-options "-O2 -Wall -ftrack-macro-expansion=0" } */
6 void sink (void*, ...);
7 #define T(x) sink (0, x)
10 test_note (int (*pia3
)[3]) // { dg-message "while referencing 'pia3'" }
16 T ((*pia3
)[i
++]); // { dg-warning "array subscript 3 is (above|outside) array bounds of 'int\\\[3]'" }
17 T ((*pia3
)[i
++]); // { dg-warning "array subscript 4 is (above|outside) array bounds of 'int\\\[3]'" }
20 /* Regrettably, the following isn't diagnosed because it's represented
21 the same as the possibly valid access below:
22 MEM[(int *)a_1(D) + 36B] = 0; */
24 T (p0
[3]); // { dg-warning "array subscript 3 is (above|outside) array bounds of 'int\\\[3]'" "pr?????" { xfail *-*-* } }
31 void test_a1_cst (_Bool (*pba0
)[0], char (*pca1
)[1],
32 short (*psa2
)[2], int (*pia3
)[3])
34 T ((*pba0
)[-1]); // { dg-warning "array subscript -1 is (above|outside) array bounds of '_Bool\\\[0]'" }
35 T ((*pba0
)[0]); // { dg-warning "array subscript 0 is (above|outside) array bounds of '_Bool\\\[0]'" }
36 T ((*pba0
)[1]); // { dg-warning "array subscript 1 is (above|outside) array bounds of '_Bool\\\[0]'" }
37 T ((*pba0
)[2]); // { dg-warning "array subscript 2 is (above|outside) array bounds of '_Bool\\\[0]'" }
38 T ((*pba0
)[12]); // { dg-warning "array subscript 12 is (above|outside) array bounds of '_Bool\\\[0]'" }
40 T ((*pca1
)[-1]); // { dg-warning "array subscript -1 is (below|outside) array bounds of 'char\\\[1]'" }
42 T ((*pca1
)[1]); // { dg-warning "array subscript 1 is (above|outside) array bounds of 'char\\\[1]'" }
43 T ((*pca1
)[2]); // { dg-warning "array subscript 2 is (above|outside) array bounds of 'char\\\[1]'" }
44 T ((*pca1
)[123]); // { dg-warning "array subscript 123 is (above|outside) array bounds of 'char\\\[1]'" }
46 T ((*psa2
)[-1]); // { dg-warning "array subscript -1 is (below|outside) array bounds of 'short int\\\[2]'" }
49 T ((*psa2
)[2]); // { dg-warning "array subscript 2 is (above|outside) array bounds of 'short int\\\[2]'" }
50 T ((*psa2
)[1234]); // { dg-warning "array subscript 1234 is (above|outside) array bounds of 'short int\\\[2]'" }
52 T ((*pia3
)[-1]); // { dg-warning "array subscript -1 is (below|outside) array bounds of 'int\\\[3]'" }
56 T ((*pia3
)[3]); // { dg-warning "array subscript 3 is (above|outside) array bounds of 'int\\\[3]'" }
57 T ((*pia3
)[12345]); // { dg-warning "array subscript 12345 is (above|outside) array bounds of 'int\\\[3]'" }
61 void test_a2_cst (_Bool (*pba0_1
)[0][1], char (*pca1_2
)[1][2],
62 short (*psa2_3
)[2][3], int (*pia3_4
)[3][4])
64 T ((*pba0_1
)[-1][-1]); // { dg-warning "array subscript -1 is (below|outside) array bounds of '_Bool\\\[1]'" }
65 T ((*pba0_1
)[-1][0]); // { dg-warning "array subscript -1 is (above|outside) array bounds of '_Bool\\\[0]\\\[1]'" }
67 T ((*pba0_1
)[0][-1]); // { dg-warning "array subscript -1 is (below|outside) array bounds of '_Bool\\\[1]'" }
68 T ((*pba0_1
)[0][0]); // { dg-warning "array subscript 0 is (above|outside) array bounds of '_Bool\\\[0]\\\[1]'" }
69 T ((*pba0_1
)[0][1]); // { dg-warning "array subscript 1 is (above|outside) array bounds of '_Bool\\\[1]'" }
70 T ((*pba0_1
)[0][2]); // { dg-warning "array subscript 2 is (above|outside) array bounds of '_Bool\\\[1]'" }
71 T ((*pba0_1
)[0][12]); // { dg-warning "array subscript 12 is (above|outside) array bounds of '_Bool\\\[1]'" }
73 T ((*pba0_1
)[1][-1]); // { dg-warning "array subscript -1 is (below|outside) array bounds of '_Bool\\\[1]'" }
74 T ((*pba0_1
)[1][0]); // { dg-warning "array subscript 1 is (above|outside) array bounds of '_Bool\\\[0]\\\[1]'" }
75 T ((*pba0_1
)[1][1]); // { dg-warning "array subscript 1 is (above|outside) array bounds of '_Bool\\\[1]'" }
76 T ((*pba0_1
)[1][2]); // { dg-warning "array subscript 2 is (above|outside) array bounds of '_Bool\\\[1]'" }
77 T ((*pba0_1
)[1][12]); // { dg-warning "array subscript 12 is (above|outside) array bounds of '_Bool\\\[1]'" }
82 T ((*pca1_2
)[0][2]); // { dg-warning "array subscript 2 is (above|outside) array bounds of 'char\\\[2]'" }
84 T ((*pca1_2
)[1][0]); // { dg-warning "array subscript 1 is (above|outside) array bounds of 'char\\\[1]\\\[2]'" }
85 T ((*pca1_2
)[1][1]); // { dg-warning "array subscript 1 is (above|outside) array bounds of 'char\\\[1]\\\[2]'" }
86 T ((*pca1_2
)[1][2]); // { dg-warning "array subscript 2 is (above|outside) array bounds of 'char\\\[2]'" }
92 T ((*psa2_3
)[0][3]); // { dg-warning "array subscript 3 is (above|outside) array bounds of 'short int\\\[3]'" }
97 T ((*psa2_3
)[1][3]); // { dg-warning "array subscript 3 is (above|outside) array bounds of 'short int\\\[3]'" }
99 T ((*psa2_3
)[2][0]); // { dg-warning "array subscript 2 is (above|outside) array bounds of 'short int\\\[2]\\\[3]'" }
100 T ((*psa2_3
)[2][1]); // { dg-warning "array subscript 2 is (above|outside) array bounds of 'short int\\\[2]\\\[3]'" }
101 T ((*psa2_3
)[2][2]); // { dg-warning "array subscript 2 is (above|outside) array bounds of 'short int\\\[2]\\\[3]'" }
102 T ((*psa2_3
)[2][3]); // { dg-warning "array subscript 3 is (above|outside) array bounds of 'short int\\\[3]'" }
109 T ((*pia3_4
)[0][4]); // { dg-warning "array subscript 4 is (above|outside) array bounds of 'int\\\[4]'" }
115 T ((*pia3_4
)[1][4]); // { dg-warning "array subscript 4 is (above|outside) array bounds of 'int\\\[4]'" }
121 T ((*pia3_4
)[2][4]); // { dg-warning "array subscript 4 is (above|outside) array bounds of 'int\\\[4]'" }
123 T ((*pia3_4
)[3][0]); // { dg-warning "array subscript 3 is (above|outside) array bounds of 'int\\\[3]\\\[4]'" }
124 T ((*pia3_4
)[3][1]); // { dg-warning "array subscript 3 is (above|outside) array bounds of 'int\\\[3]\\\[4]'" }
125 T ((*pia3_4
)[3][2]); // { dg-warning "array subscript 3 is (above|outside) array bounds of 'int\\\[3]\\\[4]'" }
126 T ((*pia3_4
)[3][3]); // { dg-warning "array subscript 3 is (above|outside) array bounds of 'int\\\[3]\\\[4]'" }
127 T ((*pia3_4
)[3][4]); // { dg-warning "array subscript 4 is (above|outside) array bounds of 'int\\\[4]'" }
132 typedef IA4 IA3_4
[3];
134 void test_a2_var (IA3_4
*pia3_4
)
137 IA4
*pia4
= &(*pia3_4
)[0];
139 T ((*pia4
)[-1]); // { dg-warning "array subscript -1 is (below|outside) array bounds of 'IA4'" }
144 T ((*pia4
)[4]); // { dg-warning "array subscript 4 is (above|outside) array bounds of 'IA4'" }
148 IA4
*pia4
= &(*pia3_4
)[1];
150 T ((*pia4
)[-1]); // { dg-warning "array subscript -1 is (below|outside) array bounds of 'IA4'" }
155 T ((*pia4
)[4]); // { dg-warning "array subscript 4 is (above|outside) array bounds of 'IA4'" }
159 IA4
*pia4
= &(*pia3_4
)[2];
161 T ((*pia4
)[-1]); // { dg-warning "array subscript -1 is (below|outside) array bounds of 'IA4'" }
166 T ((*pia4
)[4]); // { dg-warning "array subscript 4 is (above|outside) array bounds of 'IA4'" }
170 IA4
*pia4
= &(*pia3_4
)[3];
172 T ((*pia4
)[-1]); // { dg-warning "\\\[-Warray-bounds" }
173 /* The following aren't diagnosed unless N itself is out of bounds
174 because thanks to the MEM_REF they're indistinguishable from
175 possibly valid accesses:
176 MEM[(int[4] *)pia3_4_2(D) + 48B][N]; */
177 T ((*pia4
)[0]); // { dg-warning "\\\[-Warray-bounds" "pr?????" { xfail *-*-* } }
178 T ((*pia4
)[1]); // { dg-warning "\\\[-Warray-bounds" "pr?????" { xfail *-*-* } }
179 T ((*pia4
)[2]); // { dg-warning "\\\[-Warray-bounds" "pr?????" { xfail *-*-* } }
180 T ((*pia4
)[3]); // { dg-warning "\\\[-Warray-bounds" "pr?????" { xfail *-*-* } }
181 T ((*pia4
)[4]); // { dg-warning "\\\[-Warray-bounds" }
186 struct S
{ IA3_4
*pia3_4
; };
187 typedef struct S S5
[5];
190 void test_s5_7 (S5_7
*ps5_7
)
193 S5
*ps5
= &(*ps5_7
)[0];
195 T ((*(*ps5
)[0].pia3_4
)[0][0]);
196 T ((*(*ps5
)[0].pia3_4
)[2][3]);
197 T ((*(*ps5
)[0].pia3_4
)[2][4]); // { dg-warning "array subscript 4 is above array bounds of 'IA4'" }
199 T ((*(*ps5
)[1].pia3_4
)[2][3]);
200 T ((*(*ps5
)[5].pia3_4
)[2][3]); // { dg-warning "array subscript 5 is above array bounds of 'S5'" }