https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85882
Bug ID: 85882 Summary: Value of local variable changes unintentionally if certain optimization are enabled Product: gcc Version: 8.1.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: stefan.bolus at gmx dot de Target Milestone: --- In the method ... void readMessageBegin() { int32_t sz; readI32(sz); // Emulates reading from wire, `sz` passed by ref. // Here, the value of `sz` is -2147418110. std::cout << "Value before if-throw: " << sz << std::endl; int32_t version = sz & ((int32_t) 0xffff0000); if (version != ((int32_t) 0x80010000)) { // This seems to have a `throw` for the error to happen. throw 123; } // But here, the value of `sz` suddenly is -2147418112. std::cout << "Value after if-throw : " << sz << std::endl; } ... the value of `sz` changes between the `cout` line if `-O2` is turned on. If any of the optimizations `-ftree-vrp -finline -ftree-fre` is turned off, then the change does not occur. A minimal example is attached to the ticket. Additionally, a Makefile which disables most other optimizations with the error still being there, is also attached. $ g++ --version g++ (GCC) 8.1.1 20180502 (Red Hat 8.1.1-1) $ uname -a Linux blackbox 4.16.8-300.fc28.x86_64 #1 SMP Wed May 9 20:23:40 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux --- Comment #1 from Stefan Bolus <stefan.bolus at gmx dot de> --- Created attachment 44168 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=44168&action=edit Makefile to disable most optimization options.