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

            Bug ID: 103725
           Summary: warning: assuming signed overflow does not occur when
                    simplifying conditional to constant
           Product: gcc
           Version: 11.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: f.heckenb...@fh-soft.de
  Target Milestone: ---

% cat test.c  
int d = 0, b = 8, a[8][8];

struct S
{
  int c;
};

void foo (int *c)
{
  if (b >= 0)
    {
      if (*c) d = *c;
      if (*c < 0) d = *c;
    }
}

int main ()
{
  struct S i = { 0 };
  for (int e = 0; e < 11; e++, i.c++)
    {
      d = a[i.c >= b ? i.c - b : i.c][i.c + 1 >= b ? i.c + 1 - b : i.c + 1];
      foo (&i.c);
    }
}
% gcc -O2 -Wstrict-overflow test.c 
test.c: In function 'main':
test.c:22:66: warning: assuming signed overflow does not occur when simplifying
conditional to constant [-Wstrict-overflow]
   22 |       d = a[i.c >= b ? i.c - b : i.c][i.c + 1 >= b ? i.c + 1 - b : i.c
+ 1];
      |                                      
~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~

This is not the same as https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103724 (in
fact, that was a collateral damage while bisecting this one), though they may
be internally related since this warning also depends only many circumstances,
plus a few more (like, there must be a struct, a plain integer won't cause the
warning; and e and i.c must be distinct although they count in lockstep).

To a user, the warning is worrying since none of the visible conditionals
should be able to be simplified to a constant.

What seems to be happening, as far as I can tell from looking at the generated
code, is that GCC internally splits cases and then simplifies conditions in one
of them (since i.c >= b implies i.c + 1 >= b, unless signed overflow which
seems to explain the cause of the warning).

But the user normally doesn't see this and gets the impression one of their
conditions is wrongly simplified. This, and the fact that the warning doesn't
seem very robust in the first place (i.e. depends on many circumstances), seems
to make the whole "-Wstrict-overflow" option a bit less than useful, IMHO.

I get another spurious warning like this in a more complex piece of my code,
and it also seems to depend on many circumstances, so I can't easily provide a
reproducible example, but it may be a similar situation like this one in the
end. (And these were the only ones I got, i.e. it didn't actually help me find
a real bug in my code.)

Since it also doesn't seem possible to suppress the warning locally with a
pragma (since it's generated at a late stage, I suppose), I think I'll have to
disable it again. (I had re-enabled it after I heard
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70455 was fixed. It may work
better now, but still too mysterious to actually help me.)

Reply via email to