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

            Bug ID: 82583
           Summary: missing -Warray-bounds on out-of-bounds inner indices
           Product: gcc
           Version: 8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

The -Warray-bounds warning seems to only check the innermost array index but it
doesn't check any of intervening ones.  This can be seen in the test case below
where the out-of-bounds index in 'p->a[1].a[4]' in f() is detected but the
similarly out-of-bounds index in 'p->a[2]' is not.

$ cat a.c && gcc -O3 -S -Wall -Warray-bounds a.c 
struct A { int a[4]; int i; };
struct B { struct A a[2]; int i; };

void f (struct B *p)
{
  p->a[1].a[4] = 1;   // -Warray-bounds (good)
}

void g (struct B *p)
{
  p->a[2].a[3] = 1;   // missing -Warray-bounds
}

a.c: In function ‘f’:
a.c:6:12: warning: array subscript is above array bounds [-Warray-bounds]
   p->a[1].a[4] = 1;   // -Warray-bounds (good)
   ~~~~~~~~~^~~

Reply via email to