diff options
author | Nathaniel Shead <nathanieloshead@gmail.com> | 2023-07-23 01:14:37 +1000 |
---|---|---|
committer | Jason Merrill <jason@redhat.com> | 2023-07-25 21:45:10 -0400 |
commit | b8266af71c19a0bd7db4d08c8d2ee3c33214508c (patch) | |
tree | ed9f9806f590d2a2c371b44ee636c5b5e37d2882 /gcc/range-op.cc | |
parent | 5ebe5bcf8b6b6a0de16737b717e8bd06e4950a14 (diff) | |
download | gcc-b8266af71c19a0bd7db4d08c8d2ee3c33214508c.zip gcc-b8266af71c19a0bd7db4d08c8d2ee3c33214508c.tar.gz gcc-b8266af71c19a0bd7db4d08c8d2ee3c33214508c.tar.bz2 |
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>
Diffstat (limited to 'gcc/range-op.cc')
0 files changed, 0 insertions, 0 deletions