[Bug c/69612] Optimizer does not consider overflow

2016-02-02 Thread roarl at pvv dot org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69612

--- Comment #2 from Roar Lauritzsen  ---
Thanks a lot for the quick analysis. Now that I know what it is I can fix my
program, and the -fsanitize=undefined will come in handy for localizing problem
areas. For future googlers, I am planning to fix my program along these lines:

unsigned test2(unsigned a) {
if ((int)a < 0)
return 1;
a++; // 0x7FFF + 1 is not undefined anymore
if ((int)a < 0)
return 2;
return a;
}

This code executes the way I want also with -O2, and there are no errors from
-fsanitize=undefined. Personally, I find it somewhat incongruent that integer
overflow is undefined, while integer casting is not, and I also find it very
disturbing that optimized and non-optimized code may behave differently.

[Bug c/69612] New: Optimizer does not consider overflow

2016-02-01 Thread roarl at pvv dot org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69612

Bug ID: 69612
   Summary: Optimizer does not consider overflow
   Product: gcc
   Version: 5.2.1
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: c
  Assignee: unassigned at gcc dot gnu.org
  Reporter: roarl at pvv dot org
  Target Milestone: ---

I found a short code snippet that produces different results after optimizing
with -O2. I believe my code should work as it only uses a simple test to check
if incrementation has overflowed. It looks like the optimizer, having
established that a variable is >= 0, does not believe the variable can become <
0 after incrementing. However, I am relying on just that to catch the overflow.

In the code below, the __attribute__((noinline)) is not necessary to
demonstrate the "effect", however without it the "test" routine would have to
reside in another compilation unit.

% cat bug.c
int __attribute__((noinline)) test(int a) {
if (a < 0)
return 1;
a++;
if (a < 0) // gcc -O2 thinks this can't happen, since a>=0 from above
return 2;
return a;
}

#include 
int main()
{
printf("%d\n", test(0x7FFF));
}

% gcc bug.c -o bug
% ./bug
2
% gcc -O2 bug.c -o bug
% ./bug
-2147483648
% gcc --version
gcc (SUSE Linux) 5.2.1 20151008 [gcc-5-branch revision 228597]
...