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

            Bug ID: 93848
           Summary: missing -Warray-bounds warning for array subscript 1
                    is outside array bounds
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vincent-gcc at vinc17 dot net
  Target Milestone: ---

Consider the following C code:

void foo_aux (int);

void foo (void)
{
  int i;
  int *p = &i;
  foo_aux (p[1]);
}

void bar_aux (int *);

void bar (void)
{
  int i[4];
  int (*p)[4] = &i;
  bar_aux (p[1]);
}

As expected, I get a warning concerning foo:

tst.c: In function ‘foo’:
tst.c:7:3: warning: array subscript 1 is outside array bounds of ‘int[1]’
[-Warray-bounds]
    7 |   foo_aux (p[1]);
      |   ^~~~~~~~~~~~~~
tst.c:5:7: note: while referencing ‘i’
    5 |   int i;
      |       ^

but I don't get a warning concerning bar (probably because there's no memory
access in this particular case), even though this use is forbidden by the ISO C
standard. Indeed, the end of 6.5.6p8 (about the "pointer + integer" case) says:

  If the result points one past the last element of the array object,
  it shall not be used as the operand of a unary * operator that is
  evaluated.

Tested with gcc-10 (Debian 10-20200211-1) 10.0.1 20200211 (experimental), using

  gcc-10 -O3 -std=c11 -pedantic -Warray-bounds=2 -c tst.c

Reply via email to