(Checked for gcc-2.96-54, gcc-2.96-60 and CVS mainline gcc version 2.97 20001009 (experimental), but not a bug in 2.95.2) The enclosed code (which is little more than a "Hello World" program) generates incorrect code when compiled with -O2. For some reason, g++ is generating code that tramples that function arguments. Because the code doesn't seem to be particularly unusual or arcane, this seems like a very serious bug. rh7% g++ -O -o not-buggy buggy.cc && ./not-buggy * Shouldn't segfault * rh7% g++ -O2 -DAVOID_BUG -o not-buggy buggy.cc && ./not-buggy * Shouldn't segfault * rh7% g++ -O2 -o buggy buggy.cc && ./buggy Segmentation fault I hope this one won't be too hard to find and fix... M.E.O. Enc. #include <iostream> ostream& put (char * str, ostream& out) { #if !AVOID_BUG return out << "* " << str << " *\n"; #else out << "* " << str << " *\n"; return out; #endif } int main () { put("Shouldn't segfault", cout); }
Reproduced (it works well even with -O2 -fno-schedule-insns2), looking into it. Seems like it is scheduled wrongly after reload.
Actually, it was related to sibling call optimization (-fno-optimize-sibling-calls also cured it). I've submitted a fix to gcc-patches, am waiting for peer review.
Commited into CVS head, is fixed in gcc-2.96-61.