https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98467
Bug ID: 98467 Summary: gcc optimizes tapping code away Product: gcc Version: 11.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: bernd.edlinger at hotmail dot de Target Milestone: --- Consider this test case: $ cat test.cc struct MyClass; struct ptr { MyClass* get() { return t; } MyClass* t; }; struct MyClass { void call(); }; void MyClass::call() { *(char*)(nullptr) = 1; } static void intermediate(ptr p) { p.get()->call(); } int main() { intermediate(ptr{new MyClass}); } $ g++ -g -O0 test.cc $ ./a.out Segmentation fault (core dumped) $ g++ -g -Og test.cc $ ./a.out $ g++ -g -O1 test.cc $ ./a.out $ g++ -g -O2 test.cc $ ./a.out Segmentation fault (core dumped) $ g++ -g -O3 test.cc $ ./a.out Segmentation fault (core dumped) $ g++ -g -Ofast test.cc $ ./a.out Segmentation fault (core dumped) It is somehow unexpected that this code is optimized away only at -Og -O1, but not at very high or very low optimization levels. I would even say although the code is of course invalid, it should not be optimized away, as it might be a debug-code that intentionally taps.