[clang] [clang][bytecode] Check allocation size for numbers (PR #237852)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Sun Nov 1 23:18:30 PST 2026
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/237852
If the size isn't a number, we can't convert it to a numeric value.
>From 27d99a977b8fd2ad4ef93cb43828946a66688081 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
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..40454a8d1969a 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 [(long)&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