Re: Bug in GCC?

Torsten Schweigert (qiqd3@hobbit.chemie.uni-halle.de)
Wed, 26 Jul 1995 17:29:19 +0000 (GMT)


On Tue, 25 Jul 1995, Douglas Warren wrote:

> I believe I have found a serious bug in either GCC or the kernel,
> I am unable to trace it further then this, consider two files:
> test.c:
> main() {
> int i;
> double test,ANGLE;
> srandom(getpid());
> for(i=0;i<100;i++) {
> test=.5+double_rand();
> printf("%d %f\n",i,test);
> }
> }
> test1.c:
> double double_rand(void)
> {
> return (double) random() / 2147483648.0;
> }
> compiled with:
> gcc -fpcc-struct-return -O -c test1.c -o test1.o
> gcc -fpcc-struct-return -O -c test.c -o test.o
> gcc -o testa test.o test1.o -lm
> on the 7th invocation of the loop, it will crash everytime
> a sample run is:
> 0 1059284663.500000
> 1 1262909464.500000
> 2 383691163.500000
> 3 898825246.500000
> 4 976362629.500000
> 5 1993317795.500000
> Floating point exception
> HOWEVER, if the 2 files are removed, and the double_rand() function
> is in the same source function as it is called, it works flawlessly.
> I have seen this bug in multiple kernels/libaries/versions of GCC, for
> atleast a year now, but this is the first time I was able to reliably
> reproduce it. The code was tested on GCC 2.5.8, libc 4.7.2, libm 4.6.27,
> and Linux 1.2.8. Any replies would be apperiated, as well as any
> advise on where to go from here with this problem.
> --
> Douglas ``Wildcat'' Warren
> dwarren@netusa.net <---- Preferred
> dwarren@ic.sunysb.edu
> root@netusa.net
> dwarren@sunysb.edu
>
> "I hold no enmity against those coerced into evil. But to those vile
> beings that toy with the hearts and souls of men we, since the time of
> ancient gods, have been your destroyers Now, the 108th generation devil
> hunter, Yokoho is here! BEWARE!" -- Yokoho _Devil_Hunter_Yokoho_
>

I think the problem results from some forgotten include files. If test.c
and test1.c are modified as follows, everything works just fine

test.c:

#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <stdlib.h>

double double_rand(void);

main() {
int i;
double test,ANGLE,temp;
srandom(getpid());
for(i=0;i<100;i++) {
temp = double_rand();
test=0.5 + temp;
printf("%d %f\n",i,test);
}
}

test1.c:

#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <stdlib.h>

double double_rand(void)
{
double temp;
temp = (double) random() / 2147483648.0;
return temp;
}

It was compiled with

gcc -fpcc-struct-return -O -c test1.c -o test1.o
gcc -fpcc-struct-return -O -c test.c -o test.o
gcc -o testa test.o test1.o -lm

Regards -- Torsten