https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99140

            Bug ID: 99140
           Summary: missing  -Warray-bounds on a negative index into a
                    multidimensional VLA cast to one-dimensional
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

The negative subscript in the three functions below is diagnosed only when
accessing an ordinary array but not when accessing a VLA.

$ cat t.c && gcc -O2 -S -Wall t.c
void f (void*);

void f_4_8 (int n)
{
  int a[4][8];
  ((int*)a)[-1] = 0;   // -Warray-bounds
  f (a);
}

void f_4_n (int n)
{
  int a[4][n];
  ((int*)a)[-1] = 0;   // missing warning
  f (a);
}

void f_n_8 (int n)
{
  int a[n][8];
  ((int*)a)[-1] = 0;   // missing warning
  f (a);
}

t.c: In function ‘f_4_8’:
t.c:6:12: warning: array subscript -1 is outside array bounds of ‘int[4][8]’
[-Warray-bounds]
    6 |   ((int*)a)[-1] = 0;   // -Warray-bounds
      |   ~~~~~~~~~^~~~
t.c:5:7: note: while referencing ‘a’
    5 |   int a[4][8];
      |       ^

Reply via email to