https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101576
Bug ID: 101576 Summary: -fsaniitize=undefined silences clear nullptr dereference warning at compile time Product: gcc Version: 11.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: sanitizer Assignee: unassigned at gcc dot gnu.org Reporter: mail at milianw dot de CC: dodji at gcc dot gnu.org, dvyukov at gcc dot gnu.org, jakub at gcc dot gnu.org, kcc at gcc dot gnu.org, marxin at gcc dot gnu.org Target Milestone: --- take this code: ``` struct Foo { virtual ~Foo() {} }; struct Bar : Foo { ~Bar() override {}; int bar() { return _bar; } int _bar = 42; }; int doStuff(Foo *foo) { if (auto *bar = dynamic_cast<Bar *>(foo)) { return bar->bar(); } else { // this is obviously wrong, but why is there no compiler warning?! return bar->bar(); } } int main() { Foo foo; return doStuff(&foo); } ``` when you compile it with `-O -Wall -Wextra -Wpedantic -Werror` you'll get... Nothing? Why? But now also add `-Wnull-dereference` and you'll get: https://godbolt.org/z/EKn8q85nT ``` <source>: In function 'int doStuff(Foo*)': <source>:10:24: error: null pointer dereference [-Werror=null-dereference] 10 | int bar() { return _bar; } | ^~~~ cc1plus: all warnings being treated as errors Compiler returned: 1 ``` (Side note: It's unfortunate that this is not visible with -O0...) Now add `-fsanitize=undefined` and the compile error is gone again which is quite unfortunate... https://godbolt.org/z/ebeaY1vP8