[clang] [clang][bytecode] Don't produce a null type when checking new exprs (PR #110252)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 27 05:37:52 PDT 2024


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/110252

getType() might give us the right type already, so use that instead of calling getPointeeType() for all CXXNewExprs.

>From a40783d276cc0d0a6b6545182bb7264a7e7c4259 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Fri, 27 Sep 2024 14:31:49 +0200
Subject: [PATCH] [clang][bytecode] Don't produce a null type when checking new
 exprs

getType() might give us the right type already, so use that instead of
calling getPointeeType() for all CXXNewExprs.
---
 clang/lib/AST/ByteCode/Interp.cpp         |  3 ++-
 clang/test/AST/ByteCode/placement-new.cpp | 15 +++++++++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index c43f64901909ce..798e0f3e96fa09 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -1322,7 +1322,8 @@ bool CheckNewTypeMismatch(InterpState &S, CodePtr OpPC, const Expr *E,
   const auto *NewExpr = cast<CXXNewExpr>(E);
   QualType StorageType = Ptr.getType();
 
-  if (isa_and_nonnull<CXXNewExpr>(Ptr.getFieldDesc()->asExpr())) {
+  if (isa_and_nonnull<CXXNewExpr>(Ptr.getFieldDesc()->asExpr()) &&
+      StorageType->isPointerType()) {
     // FIXME: Are there other cases where this is a problem?
     StorageType = StorageType->getPointeeType();
   }
diff --git a/clang/test/AST/ByteCode/placement-new.cpp b/clang/test/AST/ByteCode/placement-new.cpp
index 1ff6ff3ac19223..caf3ac97fd1c04 100644
--- a/clang/test/AST/ByteCode/placement-new.cpp
+++ b/clang/test/AST/ByteCode/placement-new.cpp
@@ -271,3 +271,18 @@ namespace ConstructAt {
                                           // both-note {{in call}}
 
 }
+
+namespace UsedToCrash {
+  struct S {
+      int* i;
+      constexpr S() : i(new int(42)) {} // #no-deallocation
+      constexpr ~S() {delete i;}
+  };
+  consteval void alloc() {
+      S* s = new S();
+      s->~S();
+      new (s) S();
+      delete s;
+  }
+  int alloc1 = (alloc(), 0);
+}



More information about the cfe-commits mailing list