https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61781
Bug ID: 61781
Summary: Overflow of signed integer causing incorrect execution
of a program
Product: gcc
Version: 4.8.2
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: middle-end
Assignee: unassigned at gcc dot gnu.org
Reporter: geir at cray dot com
User identified a test case that may indicate an over aggressive optimizer. In
the following test case, compiler with '-O1' will result in correct execution,
while compiling with '-O2' and above results in incorrect behavior.
Here is the short version of the test case. Duplicated bug with 4.8.[012] and
4.9.0 compilers:
$ cat simple.c
#include <stdio.h>
#include <stdlib.h>
long myrand( long x )
{
long c = 6824061905351802757L; // prime
long d = 5001297291839117257L; // prime, p - 1 divisible by 4
x = d*x + c;
return( x );
}
int main( int argc, char *argv[] ) {
long x, len;
long count = 0;
x = 1;
len = 1e2;
if( argc > 1 ) {
len = atof( argv[1] ) + 0.5;
}
if( argc > 2 ) {
x = strtol( argv[2], 0, 0 );
}
printf("len = %ld, seed = 0x%016lx\n", len, x);
for( long i=0; i<len; i++ ) {
x = myrand( i ); // this version breaks gcc
printf("%4ld: 0x%016lx\n", i, x);
count++;
}
printf("Count is %ld\n", count);
return( 0 );
}
$ gcc --version
gcc (GCC) 4.9.0 20140422 (Cray Inc.)
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$
-- Compiling with -O1 works --
$ gcc -O1 -std=c99 simple.c
$ ./a.out | tail
91: 0x0abc128a0365f3f8
92: 0x50243fed93978bc1
93: 0x958c6d5123c9238a
94: 0xdaf49ab4b3fabb53
95: 0x205cc818442c531c
96: 0x65c4f57bd45deae5
97: 0xab2d22df648f82ae
98: 0xf0955042f4c11a77
99: 0x35fd7da684f2b240
Count is 100
$
-- Compiling with -O2 fails --
$ gcc -O2 -std=c99 simple.c
$ ./a.out
len = 100, seed = 0x0000000000000001
0: 0x5eb3f025c1c4ff85
Count is 0
$