Bug 34454 - [4.3 regression] Overflow check is optimized away when -O2 is enabled
: [4.3 regression] Overflow check is optimized away when -O2 is enabled
Status: RESOLVED INVALID
Product: gcc
Classification: Unclassified
Component: middle-end
: 4.3.0
: P3 normal
: ---
Assigned To: Not yet assigned to anyone
:
:
:
:
  Show dependency treegraph
 
Reported: 2007-12-13 19:32 UTC by İsmail "cartman" Dönmez
Modified: 2007-12-13 20:48 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description İsmail "cartman" Dönmez 2007-12-13 19:32:53 UTC
Testcase :

#include <sys/types.h>
#include <stdio.h>

void foo(ssize_t x)
{
 if (x >= 0) {
   if (x+x < 0) printf("Overflow\n");
}
}

main()
{
  volatile ssize_t x =2147483647;
  foo(x);
}

When compiled with -O2 it doesn't print Overflow, if you omit -O2 it prints
overflow.
gcc 3.4.6 works fine so this is a regression.
Comment 1 Andrew Pinski 2007-12-13 19:36:26 UTC
This is not a bug, signed overflow is undefined so optimizing away this case is
correct as a positive + positive is always positive.

Use -fwarpv or what ever the strict overflow option is.
Comment 2 Andrew Pinski 2007-12-13 19:37:04 UTC
Or use unsigned types to do the addition.
Comment 3 İsmail "cartman" Dönmez 2007-12-13 19:37:42 UTC
FWIW this triggers a crash in Python 2.5, see http://bugs.python.org/issue1608
Comment 4 Ian Lance Taylor 2007-12-13 20:37:37 UTC
Note that this gives a warning with -Wstrict-overflow.

And note that you get the desired behaviour if you compile with
-fno-strict-overflow.

As Pinski says, the code is simply incorrect according to standard C, but you
can use the -Wstrict-overflow and -fno-strict-overflow options to help fix
existing code bases.
Comment 5 İsmail "cartman" Dönmez 2007-12-13 20:48:48 UTC
Guido agreed on using -fwrapv hence a patch submitted, thanks for the
diagnosis.