GCC optimizer removes necessary code
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
gcc-4.5 (Ubuntu) |
New
|
Undecided
|
Unassigned |
Bug Description
GCC 4.5.2 in Ubuntu 11.04
The program below crashes when compiled with GCC 4.5.2 with optimization
level -O3. This is apparently a bug in GCC.
The crash does not occur if the program is compiled with an earlier
release of GCC, also with -O3 optimization.
The crash does not occur if the program is compiled without optimization.
The crash does not occur if a do-nothing line of code is added which
makes a reference to the variable involved. Apparently this stops GCC
from optimizing-out necessary code.
Crash matrix
GCC level Optimization dummy code result
4.4.5 -O3 no OK
4.5.2 -O0 no OK
4.5.2 -O3 no crash
4.5.2 -O3 yes OK
There is the crash report from GDB:
Program received signal SIGSEGV, Segmentation fault.
0x0000000000498051 in pvlist_remove (pv=0x306b6fc, ii=<value optimized out>) at zfuncs.cc:1774
1774 pv->list[ii-1] = pv->list[ii];
(gdb) q
Here is the relevant part of the program. The commented line will never
execute the printf() call but it does stop the program from crashing.
struct pvlist {
int max; // max. entries
int act; // actual entries
char **list; // entries
};
pvlist * pvlist_create(int max); // create pvlist
void pvlist_free(pvlist *pv); // free pvlist
int pvlist_
int pvlist_
int pvlist_find(pvlist *pv, cchar *entry); // find entry by name
int pvlist_
int pvlist_
int pvlist_count(pvlist *pv); // return entry count
int pvlist_
char * pvlist_get(pvlist *pv, int Nth); // return Nth entry (0...)
int pvlist_sort(pvlist *pv); // sort list, ascending
// remove an entry by number and repack list
int pvlist_
{
if (ii < 0 || ii >= pv->act) return -1;
zfree(
for (ii++; ii < pv->act; ii++) {
if (! pv->act) printf("meaningless reference %d",ii); // get around GCC optimization bug
pv-
}
pv->act--;
return 0;
}
ProblemType: Bug
DistroRelease: Ubuntu 11.04
Package: gcc 4:4.5.2-1ubuntu3
ProcVersionSign
Uname: Linux 2.6.38-10-generic x86_64
NonfreeKernelMo
Architecture: amd64
Date: Fri Jul 29 00:00:30 2011
InstallationMedia: Ubuntu 11.04 "Natty Narwhal" - Release amd64 (20110427.1)
ProcEnviron:
LANGUAGE=en_US:en
LANG=en_US.UTF-8
SHELL=/bin/bash
SourcePackage: gcc-defaults
UpgradeStatus: No upgrade log present (probably fresh install)
Sorry the formatting got destroyed.
It would be better if you used a monospace font for this.