This is GCC Bugzilla
This is GCC Bugzilla Version 2.20+
View Bug Activity | Format For Printing | Clone This Bug
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.
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.
Or use unsigned types to do the addition.
FWIW this triggers a crash in Python 2.5, see http://bugs.python.org/issue1608
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.
Guido agreed on using -fwrapv hence a patch submitted, thanks for the diagnosis.