https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126360
Bug ID: 126360
Summary: Null pointer dereference in unused expression in
constant evaluation not rejected.
Product: gcc
Version: 16.1.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: gongke at ios dot ac.cn
Target Milestone: ---
Consider this code:
```cpp
struct S { int a; };
constexpr int g() {
int *p = nullptr;
*p; // UB: null dereference
return 42;
}
constexpr int f() {
int S::* pm = nullptr;
S s{};
s.*pm; // UB: null member pointer access
return 0;
}
int main() {
constexpr int y = g();
constexpr int x = f();
return x;
}
```
There is a null pointer dereference in `g` and a null pointer-to-member
dereference in `f`, and both of them are unused. Since these are UBs in
constant evaluation, they should be rejected. Clang correctly rejects them
while GCC doesn't: [godbolt](https://godbolt.org/z/E5M8KMMoj)
GCC rejects the program correctly if we make of use of `*p` and `s.*pm`. Such
type of missing UB detection for unused expressions has been reported in
#102174 which focuses on arithmetic expressions.