https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108537
Bug ID: 108537
Summary: constexpr UB pointer dereference compiles if the
dereferenced value is not used
Product: gcc
Version: 12.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: ed at edwardrosten dot com
Target Milestone: ---
The following code compiles successfully:
constexpr int a(){
int* b = new int[1];
int r= &b[100]-b; //UB
b[100]; //UB
delete[] b;
return r;
}
template<int X> int N=0;
int foo(){
return N<a()>;
}
b[100] is unconditionally undefined behaviour, even though the value is never
used.
Tested on a scattering of versions (10.3, 11.2, 12.2) with -std=c++2a -O2