https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95693
Bug ID: 95693 Summary: Incorrect error from undefined behavior sanitizer Product: gcc Version: 10.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: gcc-90 at tbilles dot hu Target Milestone: --- After upgrading GCC from 7.2 to 10.1 I get a runtime error when using -fsanitize=undefined. I cannot see anything wrong with the code. It was also suggested on the gcc-help mailing list that this is a bug in the compiler. (https://gcc.gnu.org/pipermail/gcc-help/2020-June/139055.html) The code in question can be found on Compiler Explorer (also pasted at the end of this description): https://godbolt.org/z/7rAxJj It shows that different compiler versions behave differently. Version 10.1 prints "runtime error: reference binding to null pointer of type 'int'" although there is no null pointer in the code, the reference is bound to a global integer. There is a comment on line 16 that explicitly defaults the Derived constructor. If you switch the comment with line 17, both compiler versions run fine without producing the runtime error although the defaulted constructor should be exactly the same the user defined one: https://godbolt.org/z/UShm-u According to Compiler Explorer the incorrect behavior began in GCC 8.x series and is still present in trunk. --- int global = 9; class Payload { public: Payload() : data(global) {} private: int& data; }; struct Base { Payload payload; }; class Derived : public Base { public: //Derived() = default; Derived() : Base() {} Payload p; }; int main() { Derived t; }