[Bug middle-end/99129] New: missing -Warray-bounds accessing a zero-length member array of a local struct with a cast

msebor at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Tue Feb 16 23:00:05 GMT 2021


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

            Bug ID: 99129
           Summary: missing -Warray-bounds accessing a zero-length member
                    array of a local struct with a cast
           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: ---

-Warray-bounds is issued only for the first out of bounds access in the test
case below.  The same bug in h() is not diagnosed.

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

int g (int n)
{
  struct {
    int i, a[0];
  } s;

  f (&s);

  return s.a[n];   // -Warray-bounds (good)
}

int h (int n)
{
  struct {
    int i, a[0];
  } s;

  f (&s);

  return ((int*)s.a)[n];   // missing warning
}


t.c: In function ‘g’:
t.c:11:13: warning: array subscript n is outside array bounds of ‘int[0]’
[-Warray-bounds]
   11 |   return s.a[n];   // -Warray-bounds (good)
      |          ~~~^~~
t.c:6:12: note: while referencing ‘a’
    6 |     int i, a[0];
      |            ^
t.c:7:5: note: defined here ‘s’
    7 |   } s;
      |     ^


More information about the Gcc-bugs mailing list