[clang] f8724bd - [clang][bytecode] Check live-ness when calling dtors (#137645)

via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 28 08:47:34 PDT 2025


Author: Timm Baeder
Date: 2025-04-28T17:47:30+02:00
New Revision: f8724bd873878df81720bdec8b7a9a24f2a40a03

URL: https://github.com/llvm/llvm-project/commit/f8724bd873878df81720bdec8b7a9a24f2a40a03
DIFF: https://github.com/llvm/llvm-project/commit/f8724bd873878df81720bdec8b7a9a24f2a40a03.diff

LOG: [clang][bytecode] Check live-ness when calling dtors (#137645)

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

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/Interp.cpp
    clang/test/AST/ByteCode/records.cpp

Removed: 
    


################################################################################
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}}
+}


        


More information about the cfe-commits mailing list