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

            Bug ID: 105690
           Summary: -Warray-bounds sensitive false positive with -O2
           Product: gcc
           Version: 12.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: sirl at gcc dot gnu.org
  Target Milestone: ---

Hi,

this minimized testcase issues

# gcc-12 -O2 -Warray-bounds -c testcase.c
testcase.c: In function 'fun3.part.0':
testcase.c:16:13: warning: array subscript [0, 19] is outside array bounds of
'table_t[20]' [-Warray-bounds=]
   16 |   if (gtable[idx].mode)
      |       ~~~~~~^~~~~
testcase.c:8:9: note: while referencing 'gtable'
    8 | table_t gtable[20];
      |         ^~~~~~

with target linux-x86_64. It happens with gcc-12@r12-r8405 or
gcc-trunk@r13-555. The testcase is quite sensitive, even choosing a buffer size
smaller than 257 stops the warning. With gcc-7 to gcc-11 I don't get the
warning.

int gvar1;
void fun1(int, char *, int);
void fun2(int, char *);
typedef struct {
  int exist;
  int mode;
} table_t;
table_t gtable[20];

void fun3(int idx)
{
  char buffer[257];
  if (idx < 0 || idx >= 20 || gtable[idx].exist == 0)
    return;
  fun2(idx, buffer);
  if (gtable[idx].mode)
    fun2(idx, buffer);
}

void fun4()
{
  fun3(gvar1);
}

int fun2_maxlen;
void fun2(int idx, char * text)
{
  if (!(idx < 0 || idx >= 20 || gtable[idx].exist == 0))
    fun1(idx, text, fun2_maxlen);
}

Reply via email to