]> git.ipfire.org Git - thirdparty/gcc.git/commit
c++: Prevent dangling pointers from becoming nullptr in constexpr [PR110619]
authorNathaniel Shead <nathanieloshead@gmail.com>
Sat, 22 Jul 2023 15:14:37 +0000 (01:14 +1000)
committerJason Merrill <jason@redhat.com>
Wed, 26 Jul 2023 01:45:10 +0000 (21:45 -0400)
commitb8266af71c19a0bd7db4d08c8d2ee3c33214508c
treeed9f9806f590d2a2c371b44ee636c5b5e37d2882
parent5ebe5bcf8b6b6a0de16737b717e8bd06e4950a14
c++: Prevent dangling pointers from becoming nullptr in constexpr [PR110619]

Currently, when typeck discovers that a return statement will refer to a
local variable it rewrites to return a null pointer. This causes the
error messages for using the return value in a constant expression to be
unhelpful, especially for reference return values, and is also a visible
change to otherwise valid code (as in the linked PR).

The transformation is nonetheless important, however, both as a safety
guard against attackers being able to gain a handle to other data on the
stack, and to prevent duplicate warnings from later null-dereference
warning passes.

As such, this patch just delays the transformation until cp_genericize,
after constexpr function definitions have been generated.

PR c++/110619

gcc/cp/ChangeLog:

* cp-gimplify.cc (cp_genericize_r): Transform RETURN_EXPRs to
not return dangling pointers.
* cp-tree.h (RETURN_EXPR_LOCAL_ADDR_P): New flag.
(check_return_expr): Add a new parameter.
* semantics.cc (finish_return_stmt): Set flag on RETURN_EXPR
when referring to dangling pointer.
* typeck.cc (check_return_expr): Disable transformation of
dangling pointers, instead pass this information to caller.

gcc/testsuite/ChangeLog:

* g++.dg/cpp1y/constexpr-110619.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
gcc/cp/cp-gimplify.cc
gcc/cp/cp-tree.h
gcc/cp/semantics.cc
gcc/cp/typeck.cc
gcc/testsuite/g++.dg/cpp1y/constexpr-110619.C [new file with mode: 0644]