[Bug other/85398] g++ reports "array subscript is above array bounds" when it cannot be sure

2018-04-18 Thread msebor at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85398

--- Comment #4 from Martin Sebor  ---
A simpler test case is this (which is analogous to what the loop is transformed
into):

$ cat x.c && gcc -O2 -S -Wall x.c
  unsigned left[1];
  unsigned long right[1];

  void f (unsigned i)
  {
if (i)
  left[i] = right[i - 1];
  }

x.c: In function ‘f’:
x.c:7:11: warning: array subscript [0, 0] is outside array bounds of ‘unsigned
int[1]’ [-Warray-bounds]
   left[i] = right[i - 1];
   ^~~

Here it's even more obvious that the warning is wrong.

It seems to me that the whole if statement could either be eliminated or its
body replaced by a trap because the assignment in it is undefined.  That would
eliminate the loop (and with it also the warning).

[Bug other/85398] g++ reports "array subscript is above array bounds" when it cannot be sure

2018-04-18 Thread patrickdepinguin at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85398

--- Comment #3 from Thomas De Schampheleire  
---
(In reply to Richard Biener from comment #2)
> 
> We could change the warning to have a "may be above array bounds" form
> for your case but that wouldn't handle the bar() case.

The problem with giving warnings about potential-but-not-definite issues is
that projects that compile with '-Wall -Werror' assume zero warnings to guard
quality.

But if some warnings are false-positives, this strategy no longer works. The
project will fail to compile even though it is perfectly fine.

You'd need a way to tell gcc that this code is fine, or put such cases in a
separate warning category that is not included in Wall or can be disabled
explicitly.

[Bug other/85398] g++ reports "array subscript is above array bounds" when it cannot be sure

2018-04-18 Thread rguenth at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85398

--- Comment #2 from Richard Biener  ---
This is the simple case of GCC optimizing the access to a constant:

   [50.00%]:
  max.0_11 = max;
  if (max.0_11 > 1)
goto ; [50.00%]
  else
goto ; [50.00%]

   [25.00%]:
  _13 = right[0];
  left[1] = _13;

   [50.00%]:
  return;

and we warn for the case of max > 1.

Similarly we warn for

int bar () { return left[2]; }

even if we can't prove that bar() is actually executed.

We could change the warning to have a "may be above array bounds" form
for your case but that wouldn't handle the bar() case.

[Bug other/85398] g++ reports "array subscript is above array bounds" when it cannot be sure

2018-04-13 Thread msebor at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85398

Martin Sebor  changed:

   What|Removed |Added

   Keywords||diagnostic,
   ||missed-optimization
 Status|UNCONFIRMED |NEW
   Last reconfirmed||2018-04-13
 CC||msebor at gcc dot gnu.org
 Ever confirmed|0   |1

--- Comment #1 from Martin Sebor  ---
Confirmed.  It doesn't look like the assignment to left[i] affects the
determination of the maximum number of iterations of the loop during unrolling.
 This is also a missed optimization opportunity since the whole loop could be
eliminated.