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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |msebor at gcc dot gnu.org
          Component|c++                         |middle-end
             Blocks|                            |56456

--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
_Warray-bounds relies on optimization which includes removing unused
assignments as in the test case in comment #0, as well folding reads from many
objects to constants (valid or otherwise).  This makes it possible to detect
many non-trivial instances of the problem, such as when the index isn't
constant or the array is accessed via a pointer.

The Clang implementation, on the other hand, doesn't depend on optimization, so
it detects this simple case but not anything more interesting.

It's possible to enhance the warning so that it also triggers earlier and
detects even more instances of the problem, including some of those that don't
take place at runtime.  But doing that might also trigger what most would
consider false positives.  For instance:

$ cat x.c && clang -S -Wall x.c
int f (void)
{
  int a[10] = { 0 };
  int i = 0;
  return i ? a[11] : 0;
}
x.c:5:14: warning: array index 11 is past the end of the array (which contains
      10 elements) [-Warray-bounds]
  return i ? a[11] : 0;
             ^ ~~
x.c:3:3: note: array 'a' declared here
  int a[10] = { 0 };
  ^
1 warning generated.


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56456
[Bug 56456] [meta-bug] bogus/missing -Warray-bounds

Reply via email to