https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100878
Bug ID: 100878
Summary: enabling UBSAN leads to false positive `'this' pointer
is null` when casting lambda to function pointer
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: ---
```
const auto lambda = [](int i) -> int { return i + 1; };
using Callback = int (*)(int);
int run(Callback callback, int i)
{
return callback(i);
}
int main()
{
return run(lambda, 52);
}
```
compile with `-g -Og -fsanitize=undefined -Wall -Werror -Wpedantic -Wextra`
leads to:
```
<source>: In static member function 'static constexpr
int<lambda(int)>::_FUN(int)':
<source>:1:54: error: 'this' pointer is null [-Werror=nonnull]
1 | const auto lambda = [](int i) -> int { return i + 1; };
| ^
<source>:1:21: note: in a call to non-static member function '<lambda(int)>'
1 | const auto lambda = [](int i) -> int { return i + 1; };
| ^
cc1plus: all warnings being treated as errors
Compiler returned: 1
```
see also: https://godbolt.org/z/jTYYWbMsz