Bug 88992 - missing -Warray-bounds indexing into a zero-length array
Summary: missing -Warray-bounds indexing into a zero-length array
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 9.0
: P3 normal
Target Milestone: 10.0
Assignee: Martin Sebor
URL:
Keywords: diagnostic
Depends on:
Blocks: Warray-bounds
  Show dependency treegraph
 
Reported: 2019-01-22 18:17 UTC by Martin Sebor
Modified: 2020-06-10 17:41 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work: 10.1.0
Known to fail: 4.1.3, 4.3.5, 4.4.7, 4.8.5, 4.9.4, 5.4.0, 6.4.0, 7.3.0, 8.2.0, 9.0
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2019-01-22 18:17:22 UTC
Also while adding a test for the fix for bug 88956 I noticed that GCC doesn't diagnose indexing into zero-length arrays.  The out-of-bounds accesses to the global zero-length arrays are folded to zero while those to the local ones are emitted.

$ gcc -O2 -S -Wall t.c
const char s1[1] = { };
int f1 (void)
{
  return s1[3];   // -Warray-bounds (good)
}

const char s0[0] = { };
int f0 (void)
{
  return s0[3];   // missing warning
}


int g1 (void)
{
  const char s1[1] = { };
  return s1[3];   // -Warray-bounds (good)
}

int g0 (void)
{
  const char s0[0] = { };
  return s0[3];   // missing warning
}

t.c: In function ‘f1’:
t.c:4:12: warning: array subscript 3 is above array bounds of ‘const char[1]’ [-Warray-bounds]
    4 |   return s1[3];   // -Warray-bounds (good)
      |          ~~^~~
t.c:1:12: note: while referencing ‘s1’
    1 | const char s1[1] = { };
      |            ^~
t.c: In function ‘g1’:
t.c:17:12: warning: array subscript 3 is above array bounds of ‘const char[1]’ [-Warray-bounds]
   17 |   return s1[3];   // -Warray-bounds (good)
      |          ~~^~~
t.c:16:14: note: while referencing ‘s1’
   16 |   const char s1[1] = { };
      |              ^~
Comment 1 Martin Sebor 2020-06-10 17:41:12 UTC
Fixed in GCC 10.