This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug tree-optimization/82596] New: missing -Warray-bounds on an out-of-bounds index into string literal


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

            Bug ID: 82596
           Summary: missing -Warray-bounds on an out-of-bounds index into
                    string literal
           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 program below compiles successfully with no warnings despite the references
using out-of-bounds indices into the string literals.

$ cat a.c && gcc -O2 -S -Warray-bounds a.c
#define ABC "abc"
#define DEF "def"

int f (int i)
{
  if (i < 0)
    return ABC[7];   // missing -Warray-bounds
  else
    return DEF[7];   // missing -Warray-bounds
}

int g (int i)
{
  return (i < 0 ? ABC : DEF)[7];   // missing -Warray-bounds
}


In contrast to GCC, Clang emits the expected warnings:

a.c:7:12: error: array index 7 is past the end of the array (which contains 4
elements) [-Werror,-Warray-bounds]
    return ABC[7];
           ^   ~
a.c:1:13: note: expanded from macro 'ABC'
#define ABC "abc"
            ^
a.c:9:12: error: array index 7 is past the end of the array (which contains 4
elements) [-Werror,-Warray-bounds]
    return DEF[7];
           ^   ~
a.c:2:13: note: expanded from macro 'DEF'
#define DEF "def"
            ^
a.c:14:11: error: array index 7 is past the end of the array (which contains 4
elements) [-Werror,-Warray-bounds]
  return (i < 0 ? ABC : DEF)[7];
          ^                  ~
3 errors generated.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]