[clang] [clang][ExprConst] Check for array size of initlists (PR #138673)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Wed May 7 00:31:40 PDT 2025
https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/138673
>From 224ff74f7cd2460414c96e8b528e8f086535da5f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Tue, 6 May 2025 12:36:24 +0200
Subject: [PATCH] [clang][ExprConst] Check for array size of initlists
Fixes #138653
---
clang/lib/AST/ExprConstant.cpp | 10 +++++++
.../SemaCXX/constant-expression-cxx2a.cpp | 28 +++++++++++++++++++
2 files changed, 38 insertions(+)
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index e5950f461e4b2..63a94a7855a49 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -11788,6 +11788,11 @@ bool ArrayExprEvaluator::VisitCXXParenListOrInitListExpr(
LLVM_DEBUG(llvm::dbgs() << "The number of elements to initialize: "
<< NumEltsToInit << ".\n");
+ if (!Info.CheckArraySize(ExprToVisit->getExprLoc(),
+ CAT->getNumAddressingBits(Info.Ctx), NumEltsToInit,
+ /*Diag=*/true))
+ return false;
+
Result = APValue(APValue::UninitArray(), NumEltsToInit, NumElts);
// If the array was previously zero-initialized, preserve the
@@ -11919,6 +11924,11 @@ bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E,
if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(Type)) {
unsigned FinalSize = CAT->getZExtSize();
+ if (!Info.CheckArraySize(E->getExprLoc(),
+ CAT->getNumAddressingBits(Info.Ctx), FinalSize,
+ /*Diag=*/true))
+ return false;
+
// Preserve the array filler if we had prior zero-initialization.
APValue Filler =
HadZeroInit && Value->hasArrayFiller() ? Value->getArrayFiller()
diff --git a/clang/test/SemaCXX/constant-expression-cxx2a.cpp b/clang/test/SemaCXX/constant-expression-cxx2a.cpp
index 85720606fe9de..d65651d195b3b 100644
--- a/clang/test/SemaCXX/constant-expression-cxx2a.cpp
+++ b/clang/test/SemaCXX/constant-expression-cxx2a.cpp
@@ -1497,3 +1497,31 @@ namespace GH67317 {
// expected-note {{subobject of type 'const unsigned char' is not initialized}}
__builtin_bit_cast(unsigned char, *new char[3][1]);
};
+
+namespace LargeArrays {
+ constexpr unsigned kNumberOfIterations = 2000000;
+ constexpr unsigned kThreadsNumber = 2 * 8 * 1024;
+
+ /// Large array initialized by Paren/InitListExpr.
+ template <typename T, unsigned long S>
+ struct array1 {
+ using AT = T[S];
+ AT Data{};
+ constexpr array1() : Data(T()) {}
+ };
+
+ /// And initialized by a CXXConstructExpr.
+ template <typename T, unsigned long S>
+ struct array2 {
+ using AT = T[S];
+ AT Data;
+ constexpr array2() {}
+ };
+
+ template <typename T>
+ class A{};
+ int main() {
+ array1<A<short*>, kThreadsNumber * kNumberOfIterations> futures1{};
+ array2<A<short*>, kThreadsNumber * kNumberOfIterations> futures2{};
+ }
+}
More information about the cfe-commits
mailing list