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

We can't call a destructor on a dead pointer.

>From 595420d71250dece001bf9b85cc35191a4e478bb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbae...@redhat.com>
Date: Mon, 28 Apr 2025 16:55:29 +0200
Subject: [PATCH] [clang][bytecode] Check live-ness when calling dtors

We can't call a destructor on a dead pointer.
---
 clang/lib/AST/ByteCode/Interp.cpp   |  3 +++
 clang/test/AST/ByteCode/records.cpp | 11 +++++++++++
 2 files changed, 14 insertions(+)

diff --git a/clang/lib/AST/ByteCode/Interp.cpp 
b/clang/lib/AST/ByteCode/Interp.cpp
index 4d89f23401db5..62b449597bca8 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -1375,6 +1375,9 @@ static bool checkConstructor(InterpState &S, CodePtr 
OpPC, const Function *Func,
 }
 
 bool CheckDestructor(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
+  if (!CheckLive(S, OpPC, Ptr, AK_Destroy))
+    return false;
+
   // Can't call a dtor on a global variable.
   if (Ptr.block()->isStatic()) {
     const SourceInfo &E = S.Current->getSource(OpPC);
diff --git a/clang/test/AST/ByteCode/records.cpp 
b/clang/test/AST/ByteCode/records.cpp
index 9abfe6b8a15e7..c2fe3d9007480 100644
--- a/clang/test/AST/ByteCode/records.cpp
+++ b/clang/test/AST/ByteCode/records.cpp
@@ -1819,3 +1819,14 @@ namespace GlobalDtor {
     a.~A(); // both-note {{cannot modify an object that is visible outside}}
   }
 }
+
+namespace NullDtor {
+  struct S {};
+  constexpr int foo() { // both-error {{never produces a constant expression}}
+     S *s = nullptr;
+     s->~S(); // both-note 2{{destruction of dereferenced null pointer is not 
allowed in a constant expression}}
+     return 10;
+  }
+  static_assert(foo() == 10, ""); // both-error {{not an integral constant 
expression}} \
+                                  // both-note {{in call to}}
+}

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to