https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118645
Bug ID: 118645
Summary: [UBSAN] UBSAN Fails on C-Style Arrays with constexpr
User-Defined Destructors in GCC
Product: gcc
Version: 14.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: sanitizer
Assignee: unassigned at gcc dot gnu.org
Reporter: michael.gerhold at web 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
Target Milestone: ---
Created attachment 60265
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=60265&action=edit
preprocessed file
When creating a C-style array that contains elements whose type provides a
user-defined destructor, compilation fails if UBSAN is enabled.
Minimal example:
```
struct S {
constexpr S() = default;
constexpr ~S() { }
};
int main() {
static constexpr S array[10];
}
```
Compiler arguments: -std=c++20 -fsanitize=undefined
Error message:
```
: In function 'int main()':
:7:32: error: '(((const S*)(& array)) != 0)' is not a constant
expression
7 | static constexpr S array[10];
|^
Compiler returned: 1
```
Compiler Explorer link: https://godbolt.org/z/897qMb1da
However, defining the destructor of `S` as `= default` works. Disabling UBSAN
also works.