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

Frank Heckenbach <f.heckenb...@fh-soft.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|warning                     |invalid warning: iteration
                   |                            |7 invokes undefined
                   |                            |behavior

--- Comment #1 from Frank Heckenbach <f.heckenb...@fh-soft.de> ---
% cat test.c 
int d = 0, b = 8, a[8][8];

int main ()
{
  for (int c = 0; c < 11; c++)
    {
      d = a[c >= b ? c - b : c][c + 1 >= b ? c + 1 - b : c + 1];
      if (b && c)
        d = c;
    }
}
% gcc -O3 test.c
test.c: In function 'main':
test.c:7:32: warning: iteration 7 invokes undefined behavior
[-Waggressive-loop-optimizations]
    7 |       d = a[c >= b ? c - b : c][c + 1 >= b ? c + 1 - b : c + 1];
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.c:5:3: note: within this loop
    5 |   for (int c = 0; c < 11; c++)
      |   ^~~

Many things are strange about this warning:

- First, I think it's invalid -- the "?:" ensures the array indexes are in
bounds, all variables are initialized,so I don't see what would be UB.

- It claims UB occurs on iteration 7, but the warning disappears when I change
the loop condition to "c < 10" (which would also include iteration 7)

- The "if" statement with both conditions and the assignment is necessary to
trigger the warning, although it has no actual effect.

- The warning seems to apply to the 2nd array index, but already disappears
when I turn the first index into "0" without changing the 2nd one.

- It disappears when I make all variables local or all global.

Reply via email to