[clang] [clang][bytecode] Check dtor calls for one-past-end pointers (PR #140047)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Thu May 15 05:35:34 PDT 2025


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

None

>From abf8f9f5411f8ee9ee9df31ce1a5faaf0b6e8f0c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Thu, 15 May 2025 12:50:56 +0200
Subject: [PATCH] [clang][bytecode] Check dtor calls for one-past-end pointers

---
 clang/lib/AST/ByteCode/Interp.cpp | 2 ++
 clang/test/AST/ByteCode/cxx20.cpp | 8 ++++++++
 2 files changed, 10 insertions(+)

diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index fbe04fe3231dd..74efc3c914504 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -1375,6 +1375,8 @@ bool CheckDestructor(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
     return false;
   if (!CheckTemporary(S, OpPC, Ptr, AK_Destroy))
     return false;
+  if (!CheckRange(S, OpPC, Ptr, AK_Destroy))
+    return false;
 
   // Can't call a dtor on a global variable.
   if (Ptr.block()->isStatic()) {
diff --git a/clang/test/AST/ByteCode/cxx20.cpp b/clang/test/AST/ByteCode/cxx20.cpp
index 5f62a5648bab7..0b2234ef83298 100644
--- a/clang/test/AST/ByteCode/cxx20.cpp
+++ b/clang/test/AST/ByteCode/cxx20.cpp
@@ -1007,3 +1007,11 @@ namespace TempDtor {
     a_ref.~A(); // both-note {{destruction of temporary is not allowed in a constant expression outside the expression that created the temporary}}
   }
 }
+
+namespace OnePastEndDtor {
+  struct A {int n; };
+  constexpr void destroy_past_end() { // both-error {{never produces a constant expression}}
+    A a;
+    (&a+1)->~A(); // both-note {{destruction of dereferenced one-past-the-end pointer}}
+  }
+}



More information about the cfe-commits mailing list