https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118462
Bug ID: 118462
Summary: constexpr lifetime tracking mishandles variables whose
scope is reentered
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: richard-gccbugzilla at metafoo dot co.uk
Target Milestone: ---
Even after the fix for PR96630, GCC still accepts this invalid code that
accesses a variable outside its lifetime:
constexpr int f() {
int *p = 0;
for (int i = 0; i != 2; ++i) {
int n = 0;
if (i == 1) { return *p; }
p = &n;
}
}
static_assert(f() == 0);
(Live: https://godbolt.org/z/n6hznv9vT)
Each time the body of the `for` is entered, a new object is created, so `p` in
iteration 1 does not point to `n` from iteration 1 -- it points to `n` from
iteration 0, which is a different object. But GCC accepts and returns the value
of the new `n` variable.