Author: Timm Baeder
Date: 2026-06-22T13:01:58+02:00
New Revision: f3fb6fe3099887ce1538c1dbbe3694f740f9f126

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

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

We need a block pointer here for the following operations, and non-block
pointers aren't valid anyway.

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/Interp.cpp
    clang/test/AST/ByteCode/new-delete.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/Interp.cpp 
b/clang/lib/AST/ByteCode/Interp.cpp
index 106ca1b9e789e..3772def47408f 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -1390,6 +1390,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}}


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to