[clang] [clang][bytecode] Check for block pointers in Free() (PR #205043)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 22 00:27:23 PDT 2026


https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/205043

>From 041627bd1f749bcf3a1eeb51d9927332b1a2c47a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Mon, 22 Jun 2026 09:12:56 +0200
Subject: [PATCH] [clang][bytecode] Check for block pointers in Free()

We need a block pointer here for the following operations, and non-block
pointers aren't valid anyway.
---
 clang/lib/AST/ByteCode/Interp.cpp      |  3 +++
 clang/test/AST/ByteCode/new-delete.cpp | 13 +++++++++++++
 2 files changed, 16 insertions(+)

diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index 60914a2da111a..c444d0aa8e6d4 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -1380,6 +1380,9 @@ bool Free(InterpState &S, CodePtr OpPC, bool DeleteIsArrayForm,
     if (Ptr.isZero())
       return true;
 
+    if (!Ptr.isBlockPointer())
+      return false;
+
     // Remove base casts.
     QualType InitialType = Ptr.getType();
     Ptr = Ptr.expand().stripBaseCasts();
diff --git a/clang/test/AST/ByteCode/new-delete.cpp b/clang/test/AST/ByteCode/new-delete.cpp
index dd61accc898cd..43d770d5c9e61 100644
--- a/clang/test/AST/ByteCode/new-delete.cpp
+++ b/clang/test/AST/ByteCode/new-delete.cpp
@@ -1254,6 +1254,19 @@ namespace AllocInBase {
                    // both-note {{pointer to heap-allocated object is not a constant expression}}
 }
 
+namespace FreeNonBlockPointer {
+  extern int f();
+
+#define fold(x) (__builtin_constant_p(x) ? (x) : (x))
+  constexpr int foo() {
+    int *p;
+    p = fold((int*)(void*)f);
+    delete p;
+    return 10;
+  }
+  static_assert(foo() == 10); // both-error {{not an integral constant expression}}
+}
+
 #else
 /// Make sure we reject this prior to C++20
 constexpr int a() { // both-error {{never produces a constant expression}}



More information about the cfe-commits mailing list