[clang] [clang][bytecode] Fix AccessKinds in placement new CheckStore() call (PR #141123)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Thu May 22 12:05:03 PDT 2025
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/141123
CheckStore is for assignments, but we're constructing something here, so pass AK_Construct instead. We already diagnosed the test case, but as an assignment.
>From dc798c13b85c7730a99b987c334e550f11d8a51c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Thu, 22 May 2025 21:01:55 +0200
Subject: [PATCH] [clang][bytecode] Fix AccessKinds in placement new
CheckStore() call
CheckStore is for assignments, but we're constructing something here, so
pass AK_Construct instead. We already diagnosed the test case, but as an
assignment.
---
clang/lib/AST/ByteCode/Interp.cpp | 20 +++++++++++++++++++-
clang/test/AST/ByteCode/placement-new.cpp | 15 +++++++++++++--
2 files changed, 32 insertions(+), 3 deletions(-)
diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index b3ac84ce9278f..345c518b59fd3 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -1702,7 +1702,25 @@ bool CheckNewTypeMismatch(InterpState &S, CodePtr OpPC, const Expr *E,
std::optional<uint64_t> ArraySize) {
const Pointer &Ptr = S.Stk.peek<Pointer>();
- if (!CheckStore(S, OpPC, Ptr))
+ // Similar to CheckStore(), but with the additional CheckTemporary() call and
+ // the AccessKinds are different.
+ if (!CheckTemporary(S, OpPC, Ptr, AK_Construct))
+ return false;
+ if (!CheckLive(S, OpPC, Ptr, AK_Construct))
+ return false;
+ if (!CheckDummy(S, OpPC, Ptr, AK_Construct))
+ return false;
+ if (!CheckLifetime(S, OpPC, Ptr, AK_Construct))
+ return false;
+ if (!CheckExtern(S, OpPC, Ptr))
+ return false;
+ if (!CheckRange(S, OpPC, Ptr, AK_Construct))
+ return false;
+ if (!CheckGlobal(S, OpPC, Ptr))
+ return false;
+ if (!CheckConst(S, OpPC, Ptr))
+ return false;
+ if (!S.inConstantContext() && isConstexprUnknown(Ptr))
return false;
if (!InvalidNewDeleteExpr(S, OpPC, E))
diff --git a/clang/test/AST/ByteCode/placement-new.cpp b/clang/test/AST/ByteCode/placement-new.cpp
index 14b893c7e587b..4c4e23f032580 100644
--- a/clang/test/AST/ByteCode/placement-new.cpp
+++ b/clang/test/AST/ByteCode/placement-new.cpp
@@ -15,8 +15,8 @@ namespace std {
constexpr void construct_at(void *p, Args &&...args) {
new (p) T((Args&&)args...); // both-note {{in call to}} \
// both-note {{placement new would change type of storage from 'int' to 'float'}} \
- // both-note {{construction of subobject of member 'x' of union with active member 'a' is not allowed in a constant expression}}
-
+ // both-note {{construction of subobject of member 'x' of union with active member 'a' is not allowed in a constant expression}} \
+ // both-note {{construction of heap allocated object that has been deleted}}
}
}
@@ -391,3 +391,14 @@ namespace MemMove {
static_assert(foo() == 123);
}
+
+namespace PlacementNewAfterDelete {
+ constexpr bool construct_after_lifetime() {
+ int *p = new int;
+ delete p;
+ std::construct_at<int>(p); // both-note {{in call}}
+ return true;
+ }
+ static_assert(construct_after_lifetime()); // both-error {{}} \
+ // both-note {{in call}}
+}
More information about the cfe-commits
mailing list