http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49115
Summary: invalid return value optimization (?) when exception is thrown and caught Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassig...@gcc.gnu.org ReportedBy: ar...@pvv.ntnu.no we have some code that started failing when I tried upgrading our tool chain to GCC 4.5.2; it gets stale data in an object when an exception is thrown during assignment. It looks to me like return value optimization will cause the object constructor to be optimized away, but I'm not totally certain if that is the actual problem, nor what the C++ language guarantees in this situation. A self-contained test program is below, failing for me on x86_64 using: GNU C++ (GCC) version 4.5.2 (x86_64-unknown-linux-gnu) compiled by GNU C version 4.5.2, GMP version 4.3.2, MPFR version 3.0.1, MPC version 0.9 ----- test program follows ----- extern "C" { extern long write(int, const void *, unsigned long); } struct MyException {}; int simfail = 7; struct Data { int nr; Data() : nr(66) {} }; Data getData(int i) { if (simfail == i) throw MyException(); Data data; data.nr = i; return data; } bool verify() { char bad[7] = "BAD x\n"; for (int i = 0; i < 10; i++) { Data data; try { data = getData(i); } catch (MyException& e) { if (data.nr != 66) { bad[4] = '0' + data.nr; write(2, bad, 6); return false; } } } return true; } int main(int, char **) { simfail = 4; return verify() ? 0 : 1; }