[clang] [clang][bytecode] Check pointer lifetime in CheckDestructor (PR #179957)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Thu Feb 5 07:12:19 PST 2026
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/179957
So we diagnose double-destroy scenarios.
>From c297154e333798648f64bec742acff6cbadaebd8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Thu, 5 Feb 2026 16:11:20 +0100
Subject: [PATCH] [clang][bytecode] Check pointer lifetime in CheckDestructor
So we diagnose double-destroy scenarios.
---
clang/lib/AST/ByteCode/Interp.cpp | 2 ++
clang/test/AST/ByteCode/records.cpp | 9 +++++++++
2 files changed, 11 insertions(+)
diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index d095e6f862fc5..8eaff4bb07f7d 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -1549,6 +1549,8 @@ bool CheckDestructor(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
return false;
if (!CheckRange(S, OpPC, Ptr, AK_Destroy))
return false;
+ if (!CheckLifetime(S, OpPC, Ptr.getLifetime(), AK_Destroy))
+ return false;
// Can't call a dtor on a global variable.
if (Ptr.block()->isStatic()) {
diff --git a/clang/test/AST/ByteCode/records.cpp b/clang/test/AST/ByteCode/records.cpp
index 4799ebe25dde1..804b4c6ea466a 100644
--- a/clang/test/AST/ByteCode/records.cpp
+++ b/clang/test/AST/ByteCode/records.cpp
@@ -602,6 +602,15 @@ namespace Destructors {
}
static_assert(testS() == 1); // both-error {{not an integral constant expression}} \
// both-note {{in call to 'testS()'}}
+
+ struct A { int n; };
+ constexpr void double_destroy() {
+ A a;
+ a.~A();
+ a.~A(); // both-note {{destruction of object outside its lifetime}}
+ }
+ static_assert((double_destroy(), true)); // both-error {{not an integral constant expression}} \
+ // both-note {{in call to}}
}
namespace BaseToDerived {
More information about the cfe-commits
mailing list