Red Hat Bugzilla – Bug 187540
Using any explicit optimization level results in an executable that fails with 'segmentation fault.'
Last modified: 2007-11-30 17:11:29 EST
Description of problem:
Compiling source 'greader.c' with default optimization, program compiles
cleanly with no errors or warnings, and runs properly. Trying to compile with
explicit optimization (as 'gcc -lncurses -O2 greader.c'), program still compiles
and exits cleanly with no errors or warnings. However, resulting executable
fails on invocation with error message 'segmentation fault'.
This happens when run on a Compaq Presario 5050 under Fedora
Core(2.6.11-1.1369_FC4), using gcc version 4.0.0 20050519 (Red Hat 4.0.0-8).
This last according to 'gcc --version'; dmesg gives a slightly different 'gcc
version 4.0.0 20050525 (Red Hat 4.0.0-9)'.
When run under Slackware 10 (Linux 2.4.26) with gcc version 3.3.4, this
code compiles and runs properly with or without explicit optimization settings.
I didn't fit in everything below; just let me say that after unfolding the
attached tarball, you should be able to invoke the program with
Version-Release number of selected component (if applicable):
Steps to Reproduce:
Created attachment 127149 [details]
Tarball will create directory 'bugstash' which contains source greader.c and data files needed to run the executable. Also included are two executables from my machine, with/without -O2
That's just buggy testcase:
in download_roster, there is
(not initialized in any way), then:
for (a = 0; a < Class_size; a++)
strcpy (Buf, "/tmp/bugstash/Name");
zo = (char)((a / 10) + 48);
strcat (Buf, &zo);
zo = (char) ((a % 10) + 48);
strcat (Buf, &zo);
fp = fopen(Buf, "r");
fgets (Name[a], 500, fp);
That triggers undefined behavior, can work only if zo happens to have value 0,
but nothing initialized it that way.