Skip to content

Commit

Permalink
[clang][dataflow] Fix crash on unions introduced in ba27993 (#81918)
Browse files Browse the repository at this point in the history
The commit was itself a crash fix, but inadvertently changed the
behavior for unions, which results in crashes.
  • Loading branch information
ymand committed Feb 15, 2024
1 parent dc3258c commit 60cb09b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
6 changes: 6 additions & 0 deletions clang/lib/Analysis/FlowSensitive/Transfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,12 @@ class TransferVisitor : public ConstStmtVisitor<TransferVisitor> {
void VisitInitListExpr(const InitListExpr *S) {
QualType Type = S->getType();

if (Type->isUnionType()) {
if (auto *Val = Env.createValue(Type))
Env.setValue(*S, *Val);
return;
}

if (!Type->isStructureOrClassType()) {
// Until array initialization is implemented, we don't need to care about
// cases where `getNumInits() > 1`.
Expand Down
21 changes: 21 additions & 0 deletions clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2367,6 +2367,27 @@ TEST(TransferTest, InitListExprAsXValue) {
});
}

TEST(TransferTest, InitListExprAsUnion) {
// This is a crash repro.
std::string Code = R"cc(
class target {
union {
int *a;
bool *b;
} F;
public:
constexpr target() : F{nullptr} {}
};
)cc";
runDataflow(
Code,
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
ASTContext &ASTCtx) {
// Just verify that it doesn't crash.
});
}

TEST(TransferTest, CopyConstructor) {
std::string Code = R"(
struct A {
Expand Down

0 comments on commit 60cb09b

Please sign in to comment.