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

>From 932e6adaf798be5a38724b955d927369fe2df245 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <[email protected]>
Date: Mon, 2 Nov 2026 08:13:50 +0100
Subject: [PATCH] [clang][bytecode] Check allocation size for numbers

If the size isn't a number, we can't convert it to a numeric value.
---
 clang/lib/AST/ByteCode/InterpHelpers.h |  3 +++
 clang/test/AST/ByteCode/new-delete.cpp | 11 +++++++++++
 2 files changed, 14 insertions(+)

diff --git a/clang/lib/AST/ByteCode/InterpHelpers.h 
b/clang/lib/AST/ByteCode/InterpHelpers.h
index 8a99406eb1b67..e6e9d73ee1540 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);
+
   // FIXME: Both the SizeT::from() as well as the
   // NumElements.toAPSInt() in this function are rather expensive.
 
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}}

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

Reply via email to