[clang] df73624 - [clang][bytecode] Check allocation size for numbers (#237852)

via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 2 08:50:56 PST 2026


Author: Timm Baeder
Date: 2026-11-02T17:50:51+01:00
New Revision: df73624137100b1ead7bd46af7c9824c0c7c22d4

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

LOG: [clang][bytecode] Check allocation size for numbers (#237852)

If the size isn't a number, we can't convert it to a numeric value.

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/InterpHelpers.h b/clang/lib/AST/ByteCode/InterpHelpers.h
index ae34fca812c5e..590ebc2a2a5c1 100644
--- a/clang/lib/AST/ByteCode/InterpHelpers.h
+++ b/clang/lib/AST/ByteCode/InterpHelpers.h
@@ -157,6 +157,9 @@ bool CheckArraySize(InterpState &S, CodePtr OpPC, SizeT NumElements,
   if (ElemSize == 0)
     return true;
 
+  if (!NumElements.isNumber())
+    return Invalid(S, OpPC);
+
   // Can't be too many elements if the bitwidth of NumElements is lower than
   // that of Descriptor::MaxArrayElemBytes.
   if ((NumElements.bitWidth() - NumElements.isSigned()) <

diff  --git a/clang/test/AST/ByteCode/new-delete.cpp b/clang/test/AST/ByteCode/new-delete.cpp
index a9a297a10e76c..ac75913295fb7 100644
--- a/clang/test/AST/ByteCode/new-delete.cpp
+++ b/clang/test/AST/ByteCode/new-delete.cpp
@@ -1322,6 +1322,17 @@ namespace ZeroSizeElems {
   static_assert(foo());
 }
 
+namespace NonNumber {
+  constexpr int m() { // both-error {{never produces a constant expression}}
+    int baz;
+    int *p = new int [(__UINTPTR_TYPE__)&baz]; // both-note {{cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression}} \
+                                               // both-note {{subexpression not valid in a constant expression}}
+    return 0;
+  }
+  static_assert(m() == 0); // both-error {{not an integral constant expression}} \
+                           // both-note {{in call to}}
+}
+
 #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