Skip to content

Commit

Permalink
[clang][dataflow] Fix buggy assertion: Compare an unqualified type to…
Browse files Browse the repository at this point in the history
… an unqualified type. (#71573)

Includes crash-reproducing test case.

---------

Co-authored-by: martinboehme <mboehme@google.com>
  • Loading branch information
bazuzi and martinboehme committed Nov 9, 2023
1 parent c9017bc commit 3001d6d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 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 @@ -683,11 +683,11 @@ class TransferVisitor : public ConstStmtVisitor<TransferVisitor> {
assert(
// The types are same, or
Field->getType().getCanonicalType().getUnqualifiedType() ==
Init->getType().getCanonicalType() ||
Init->getType().getCanonicalType().getUnqualifiedType() ||
// The field's type is T&, and initializer is T
(Field->getType()->isReferenceType() &&
Field->getType().getCanonicalType()->getPointeeType() ==
Init->getType().getCanonicalType()));
Field->getType().getCanonicalType()->getPointeeType() ==
Init->getType().getCanonicalType()));
auto& Loc = Env.createObject(Field->getType(), Init);
FieldLocs.insert({Field, &Loc});
}
Expand Down
20 changes: 20 additions & 0 deletions clang/unittests/Analysis/FlowSensitive/TransferTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3197,6 +3197,26 @@ TEST(TransferTest, AggregateInitialization_NotExplicitlyInitializedField) {
});
}

TEST(TransferTest, AggregateInitializationFunctionPointer) {
// This is a repro for an assertion failure.
// nullptr takes on the type of a const function pointer, but its type was
// asserted to be equal to the *unqualified* type of Field, which no longer
// included the const.
std::string Code = R"(
struct S {
void (*const Field)();
};
void target() {
S s{nullptr};
}
)";
runDataflow(
Code,
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
ASTContext &ASTCtx) {});
}

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

0 comments on commit 3001d6d

Please sign in to comment.