Skip to content

Commit 2730a5c

Browse files
authored
[clang][dataflow] Skip array types when handling InitListExprs. (#83013)
Crashes resulted from single-element InitListExprs for arrays with elements of a record type after #80970.
1 parent c27d708 commit 2730a5c

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

clang/lib/Analysis/FlowSensitive/Transfer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -671,9 +671,9 @@ class TransferVisitor : public ConstStmtVisitor<TransferVisitor> {
671671
}
672672

673673
if (!Type->isStructureOrClassType()) {
674-
// Until array initialization is implemented, we don't need to care about
675-
// cases where `getNumInits() > 1`.
676-
if (S->getNumInits() == 1)
674+
// Until array initialization is implemented, we skip arrays and don't
675+
// need to care about cases where `getNumInits() > 1`.
676+
if (!Type->isArrayType() && S->getNumInits() == 1)
677677
propagateValueOrStorageLocation(*S->getInit(0), *S, Env);
678678
return;
679679
}

clang/unittests/Analysis/FlowSensitive/TransferTest.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2367,6 +2367,21 @@ TEST(TransferTest, InitListExprAsXValue) {
23672367
});
23682368
}
23692369

2370+
TEST(TransferTest, ArrayInitListExprOneRecordElement) {
2371+
// This is a crash repro.
2372+
std::string Code = R"cc(
2373+
struct S {};
2374+
2375+
void target() { S foo[] = {S()}; }
2376+
)cc";
2377+
runDataflow(
2378+
Code,
2379+
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
2380+
ASTContext &ASTCtx) {
2381+
// Just verify that it doesn't crash.
2382+
});
2383+
}
2384+
23702385
TEST(TransferTest, InitListExprAsUnion) {
23712386
// This is a crash repro.
23722387
std::string Code = R"cc(
@@ -3414,7 +3429,7 @@ TEST(TransferTest, AggregateInitializationFunctionPointer) {
34143429
struct S {
34153430
void (*const Field)();
34163431
};
3417-
3432+
34183433
void target() {
34193434
S s{nullptr};
34203435
}

0 commit comments

Comments
 (0)