[Bug c/97261] distinguish invalid subscripts from invalid addresses in -Warray-bounds

msebor at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Thu Oct 1 15:18:21 GMT 2020


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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
            Summary|gcc-10 produces invalid     |distinguish invalid
                   |-Warray-bounds warning      |subscripts from invalid
                   |                            |addresses in -Warray-bounds
   Last reconfirmed|                            |2020-10-01
           Severity|normal                      |enhancement

--- Comment #5 from Martin Sebor <msebor at gcc dot gnu.org> ---
Where pointers (as opposed to arrays) are involved, the IL doesn't
differentiate taking the address of a subscript expression like &pb[-1] from
pointer subtraction like (pb - 1).  So changing the text of the warning, while
possible,  will come with a trade-off: losing the part about the
invalid/negative subscript in the former cases.

That said, the current detection of out of bounds pointers is incomplete and
could stand to be enhanced to also detect the case below.  Let me look into
adjusting the wording when I work on that.

$ cat x.c && gcc -O2 -S -Wall x.c 
int a[2];

int* f (int i)
{
  i = 3;
  return a + i;   // warning
}

int* g (int i)
{
  if (i < 3)
    i = 3;
  return a + i;   // missing warning
}

x.c: In function ‘f’:
x.c:6:12: warning: array subscript 3 is outside array bounds of ‘int[2]’
[-Warray-bounds]
    6 |   return a + i;
      |          ~~^~~
x.c:1:5: note: while referencing ‘a’
    1 | int a[2];
      |     ^


More information about the Gcc-bugs mailing list