Re: Bug in GCC? -> NO

Manuel J. Galan Moreno (root@mgmux.ext.ulpgc.es)
Sun, 30 Jul 1995 15:54:41 +0000 (GMT)


On Tue, 25 Jul 1995, Douglas Warren wrote:

> Date: Tue, 25 Jul 1995 22:46:52 -0400 (EDT)
> From: Douglas Warren <dwarren@ic.sunysb.edu>
> To: linux-kernel@vger.rutgers.edu, linux-gcc@vger.rutgers.edu
> Subject: Bug in GCC?
>
> 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.

I did:

root@mgmux(19)[~/tmp]# cat a.c
#include <stdio.h> /* printf */
#include <stdlib.h> /* srandom */
#include <unistd.h> /* getpid */

int main(void) { /* one of the standard declarations of main */
int i;
double test,ANGLE;
double double_rand(void); /* prototype */

srandom(getpid());
for(i=0;i<100;i++) {
test=.5+double_rand();
printf("%d %f\n",i,test);
}
return EXIT_SUCCESS; /* main returns int ... */
}

...AND

root@mgmux(20)[~/tmp]# cat b.c
#include <stdlib.h> /* random */
double double_rand(void)
{
return (double) random() / 2147483648.0;
}

...AND

root@mgmux(28)[~/tmp]# gcc -O6 -Wall -Wstrict-prototypes -o kk a.c b.c
a.c: In function `main':
a.c:6: warning: unused variable `ANGLE'

...AND

root@mgmux(28)[~/tmp]# ./kk
0 1.428581
1 0.603919
2 0.813622
3 1.043047
4 0.759768
5 0.534384
6 0.663148
7 0.969980
8 0.997748
9 0.684451
10 0.755382
11 0.782935
12 1.012730
13 1.468256
14 1.097235
15 1.438768
16 0.594165
17 0.936349
18 0.847087
19 1.021495
20 1.020598
21 0.777905
22 0.536040
23 0.573571
24 1.479276
25 1.089551
26 0.889944
27 1.248240
28 0.729302
29 1.422395
30 0.605769
31 0.657883
32 0.526315
33 0.919391
34 1.200930
35 0.786083
36 0.953775
37 1.364077
38 1.256063
39 1.451523
40 0.548528
41 0.511445
42 0.734458
43 1.061258
44 1.479700
45 1.331693
46 1.000026
47 0.573866
48 0.768042
49 1.347113
50 1.095360
51 1.288640
52 0.625018
53 1.131401
54 1.362211
55 0.604294
56 0.720952
57 0.752155
58 1.352534
59 0.950254
60 0.674551
61 1.458303
62 1.108137
63 0.700866
64 0.877693
65 0.809066
66 0.986948
67 1.331468
68 0.673143
69 0.743011
70 1.282991
71 0.721672
72 0.754456
73 0.517449
74 1.282930
75 0.734156
76 1.349142
77 0.782956
78 0.808022
79 0.617184
80 0.630068
81 1.403382
82 1.405824
83 0.755086
84 1.034783
85 1.268035
86 0.859380
87 1.255735
88 0.520190
89 0.711913
90 0.705988
91 0.694741
92 0.670216
93 1.314125
94 0.895607
95 1.047909
96 0.623191
97 1.382555
98 0.879377
99 0.796335

My advice: ALWAYS include headers AND ALWAYS prototype functions returning
non 'int' values... ( -Wall -Wstrict-prototypes )

Hope this will help you...

Manuel J. Galan
root@mgmux.ext.ulpgc.es