Skip to content

Commit 224ff74

Browse files
committed
[clang][ExprConst] Check for array size of initlists
Fixes llvm#138653
1 parent 8286378 commit 224ff74

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

clang/lib/AST/ExprConstant.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -11788,6 +11788,11 @@ bool ArrayExprEvaluator::VisitCXXParenListOrInitListExpr(
1178811788
LLVM_DEBUG(llvm::dbgs() << "The number of elements to initialize: "
1178911789
<< NumEltsToInit << ".\n");
1179011790

11791+
if (!Info.CheckArraySize(ExprToVisit->getExprLoc(),
11792+
CAT->getNumAddressingBits(Info.Ctx), NumEltsToInit,
11793+
/*Diag=*/true))
11794+
return false;
11795+
1179111796
Result = APValue(APValue::UninitArray(), NumEltsToInit, NumElts);
1179211797

1179311798
// If the array was previously zero-initialized, preserve the
@@ -11919,6 +11924,11 @@ bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E,
1191911924
if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(Type)) {
1192011925
unsigned FinalSize = CAT->getZExtSize();
1192111926

11927+
if (!Info.CheckArraySize(E->getExprLoc(),
11928+
CAT->getNumAddressingBits(Info.Ctx), FinalSize,
11929+
/*Diag=*/true))
11930+
return false;
11931+
1192211932
// Preserve the array filler if we had prior zero-initialization.
1192311933
APValue Filler =
1192411934
HadZeroInit && Value->hasArrayFiller() ? Value->getArrayFiller()

clang/test/SemaCXX/constant-expression-cxx2a.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -1497,3 +1497,31 @@ namespace GH67317 {
14971497
// expected-note {{subobject of type 'const unsigned char' is not initialized}}
14981498
__builtin_bit_cast(unsigned char, *new char[3][1]);
14991499
};
1500+
1501+
namespace LargeArrays {
1502+
constexpr unsigned kNumberOfIterations = 2000000;
1503+
constexpr unsigned kThreadsNumber = 2 * 8 * 1024;
1504+
1505+
/// Large array initialized by Paren/InitListExpr.
1506+
template <typename T, unsigned long S>
1507+
struct array1 {
1508+
using AT = T[S];
1509+
AT Data{};
1510+
constexpr array1() : Data(T()) {}
1511+
};
1512+
1513+
/// And initialized by a CXXConstructExpr.
1514+
template <typename T, unsigned long S>
1515+
struct array2 {
1516+
using AT = T[S];
1517+
AT Data;
1518+
constexpr array2() {}
1519+
};
1520+
1521+
template <typename T>
1522+
class A{};
1523+
int main() {
1524+
array1<A<short*>, kThreadsNumber * kNumberOfIterations> futures1{};
1525+
array2<A<short*>, kThreadsNumber * kNumberOfIterations> futures2{};
1526+
}
1527+
}

0 commit comments

Comments
 (0)