https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122761
Bug ID: 122761
Summary: -Wnonnull false negative when NOT compiling with -O2
-fsanitize=undefined -fno-inline
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: moncef.mechri at gmail dot com
Target Milestone: ---
Consider the following code:
struct S
{
int GetA()
{
return a;
}
int a;
};
void foo(S* s)
{
if (!s)
s->GetA();
}
int main()
{
S* s = nullptr;
foo(s);
}
With g++ 14/15/16 trunk (haven't tried other versions) with -O2 -Wnonnull
-fno-inline -fsanitize=undefined, the null pointer dereference is correctly
reported:
<source>: In function 'void foo(S*)':
<source>:14:16: error: 'this' pointer is null [-Werror=nonnull]
14 | s->GetA();
| ~~~~~~~^~
<source>:3:9: note: in a call to non-static member function 'int S::GetA()'
3 | int GetA()
| ^~~~
However, if the optimization level is lowered, or if any of -fno-inline
-fsanitize=undefined is removed, the null pointer dereference is unfortunately
not reported anymore.
Demo: https://godbolt.org/z/G9PY79s11
Possibly related to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98109