https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119162
--- Comment #2 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The trunk branch has been updated by Jason Merrill <ja...@gcc.gnu.org>: https://gcc.gnu.org/g:17ef5cad94d3f8f5fb1d8b749adf04c9d775ab9c commit r15-7973-g17ef5cad94d3f8f5fb1d8b749adf04c9d775ab9c Author: Jason Merrill <ja...@redhat.com> Date: Mon Mar 10 14:10:52 2025 -0400 c++: constexpr caching deleted pointer [PR119162] In this testcase, we pass the checks for mismatched new/delete because the pointer is deleted before it is returned. And then a subsequent evaluation uses the cached value, but the deleted heap var isn't in ctx->global->heap_vars anymore, so cxx_eval_outermost_constant_expr doesn't run find_heap_var_refs, and ends up with garbage. Fixed by not caching a reference to deleted. I considered rejecting such a reference immediately as non-constant, but I don't think that's valid; an invalid pointer value isn't UB until we try to do something with it or it winds up in the final result of constant evaluation. I also considered not caching other heap references (i.e. using find_heap_var_refs instead of adding find_deleted_heap_var), which would include heap pointers passed in from the caller, but those don't have the same heap_vars problem. We might want cxx_eval_outermost_constant_expr to prune constexpr_call entries that refer to objects created during the evaluation, but that applies to local variables and temporaries just as much as heap "variables". PR c++/119162 gcc/cp/ChangeLog: * constexpr.cc (find_deleted_heap_var): New. (cxx_eval_call_expression): Don't cache a reference to heap_deleted. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/constexpr-new26.C: New test.