1 /* PR tree-optimization/82588 - missing -Warray-bounds on an excessively
2    large index
3    { dg-do compile }
4    { dg-require-effective-target alloca }
5    { dg-options "-O2 -Warray-bounds -ftrack-macro-expansion=0" }  */
6 
7 #define SIZE_MAX  __SIZE_MAX__
8 #define DIFF_MAX __PTRDIFF_MAX__
9 #define DIFF_MIN (-DIFF_MAX - 1)
10 
11 typedef __PTRDIFF_TYPE__ ptrdiff_t;
12 typedef __SIZE_TYPE__    size_t;
13 
signed_value(void)14 static ptrdiff_t signed_value (void)
15 {
16   extern volatile ptrdiff_t signed_value_source;
17   return signed_value_source;
18 }
19 
signed_range(ptrdiff_t min,ptrdiff_t max)20 static ptrdiff_t signed_range (ptrdiff_t min, ptrdiff_t max)
21 {
22   ptrdiff_t val = signed_value ();
23   return val < min || max < val ? min : val;
24 }
25 
26 typedef struct AX { int n; char ax[]; } AX;
27 
28 typedef struct A1 { int i; char a1[1]; } A1;
29 typedef struct B { int i; struct A1 a1x[]; } B;
30 
31 void sink (int, ...);
32 
33 #define T(expr)   sink (0, (expr))
34 
test_vla(unsigned m,unsigned n)35 void test_vla (unsigned m, unsigned n)
36 {
37   char vla1[m];
38 
39   T (vla1[DIFF_MIN]);                     /* { dg-warning "array subscript -\[0-9\]+ is below array bounds" "vla" } */
40   T (vla1[-1]);                           /* { dg-warning "array subscript -1 is below array bounds" "vla" } */
41   T (vla1[0]);
42   T (vla1[1]);
43   T (vla1[m - 1]);
44   /* It would be nice to diagnose this. */
45   T (vla1[m]);                            /* { dg-warning "array subscript \[0-9\]+ is above array bounds" "bug 82608" { xfail *-*-*} } */
46   T (vla1[DIFF_MAX - 1]);
47   T (vla1[DIFF_MAX]);                     /* { dg-warning "array subscript \[0-9\]+ is above array bounds" "vla" } */
48 
49   ptrdiff_t i = signed_range (DIFF_MAX - 1, DIFF_MAX);
50   T (vla1[i]);
51 
52   char vla2[m][n];
53 
54   T (vla2[0][DIFF_MIN]);                  /* { dg-warning "array subscript -\[0-9\]+ is below array bounds" "vla" } */
55   T (vla2[0][-1]);                        /* { dg-warning "array subscript -1 is below array bounds" "vla" } */
56   T (vla2[0][0]);
57   T (vla2[1][1]);
58   T (vla2[m - 1][n - 1]);
59   /* It would be nice to diagnose this. */
60   T (vla2[m][0]);                         /* { dg-warning "array subscript \[0-9\]+ is above array bounds" "bug 82608" { xfail *-*-*} } */
61   T (vla2[m + 1][0]);                     /* { dg-warning "array subscript \[0-9\]+ is above array bounds" "bug 82608" { xfail *-*-*} } */
62   T (vla2[0][n]);                         /* { dg-warning "array subscript \[0-9\]+ is above array bounds" "bug 82608" { xfail *-*-*} } */
63   T (vla2[0][n + 1]);                     /* { dg-warning "array subscript \[0-9\]+ is above array bounds" "bug 82608" { xfail *-*-*} } */
64   T (vla2[m][n]);                         /* { dg-warning "array subscript \[0-9\]+ is above array bounds" "bug 82608" { xfail *-*-*} } */
65   T (vla2[m + 1][n + 1]);                 /* { dg-warning "array subscript \[0-9\]+ is above array bounds" "bug 82608" { xfail *-*-*} } */
66 
67   T (vla2[0][DIFF_MAX]);                  /* { dg-warning "array subscript \[0-9\]+ is above array bounds" "vla" } */
68   T (vla2[DIFF_MAX][0]);                  /* { dg-warning "array subscript \[0-9\]+ is above array bounds" "vla" { xfail *-*-* } } */
69   T (vla2[DIFF_MAX][DIFF_MAX]);           /* { dg-warning "array subscript \[0-9\]+ is above array bounds" "vla" } */
70 
71   struct S256 { char a[256]; } vla3[m];
72 
73   T (vla3[DIFF_MIN].a[0]);                /* { dg-warning "array subscript -\[0-9\]+ is below array bounds" "vla" } */
74   T (vla3[-1].a[0]);                      /* { dg-warning "array subscript -1 is below array bounds" "vla" } */
75   T (vla3[0].a[0]);
76   T (vla3[1].a[0]);
77   T (vla3[m - 1].a[0]);
78   T (vla3[DIFF_MAX / 256 - 1].a[0]);
79   T (vla3[DIFF_MAX / 256].a[0]);          /* { dg-warning "array subscript \[0-9\]+ is above array bounds" "vla" } */
80 
81   i = signed_range (DIFF_MAX / 256 - 1, DIFF_MAX);
82   T (vla3[i].a[0]);
83 
84   i = signed_range (DIFF_MAX / 256, DIFF_MAX);
85   T (vla3[i].a[0]);                       /* { dg-warning "array subscript \[0-9\]+ is above array bounds" "vla" } */
86 
87   struct VLA { char vla[n]; } x;
88 
89   T (x.vla[DIFF_MIN]);                    /* { dg-warning "array subscript -\[0-9\]+ is below array bounds" "vla" } */
90   T (x.vla[-1]);                          /* { dg-warning "array subscript -1 is below array bounds" "vla" } */
91   T (x.vla[0]);
92   T (x.vla[1]);
93   T (x.vla[n - 1]);
94   T (x.vla[DIFF_MAX - 1]);
95   T (x.vla[DIFF_MAX]);                    /* { dg-warning "array subscript \[0-9\]+ is above array bounds" "vla" } */
96 }
97