Skip to content

Commit

Permalink
[clang][dataflow] Skip array types when handling InitListExprs. (#83013)
Browse files Browse the repository at this point in the history
Crashes resulted from single-element InitListExprs for arrays with
elements of a record type after #80970.
  • Loading branch information
bazuzi committed Feb 26, 2024
1 parent c27d708 commit 2730a5c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
6 changes: 3 additions & 3 deletions clang/lib/Analysis/FlowSensitive/Transfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -671,9 +671,9 @@ class TransferVisitor : public ConstStmtVisitor<TransferVisitor> {
}

if (!Type->isStructureOrClassType()) {
// Until array initialization is implemented, we don't need to care about
// cases where `getNumInits() > 1`.
if (S->getNumInits() == 1)
// Until array initialization is implemented, we skip arrays and don't
// need to care about cases where `getNumInits() > 1`.
if (!Type->isArrayType() && S->getNumInits() == 1)
propagateValueOrStorageLocation(*S->getInit(0), *S, Env);
return;
}
Expand Down
17 changes: 16 additions & 1 deletion clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2367,6 +2367,21 @@ TEST(TransferTest, InitListExprAsXValue) {
});
}

TEST(TransferTest, ArrayInitListExprOneRecordElement) {
// This is a crash repro.
std::string Code = R"cc(
struct S {};
void target() { S foo[] = {S()}; }
)cc";
runDataflow(
Code,
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
ASTContext &ASTCtx) {
// Just verify that it doesn't crash.
});
}

TEST(TransferTest, InitListExprAsUnion) {
// This is a crash repro.
std::string Code = R"cc(
Expand Down Expand Up @@ -3414,7 +3429,7 @@ TEST(TransferTest, AggregateInitializationFunctionPointer) {
struct S {
void (*const Field)();
};
void target() {
S s{nullptr};
}
Expand Down

0 comments on commit 2730a5c

Please sign in to comment.