Skip to content

Commit 3001d6d

Browse files
bazuzimartinboehme
andauthored
[clang][dataflow] Fix buggy assertion: Compare an unqualified type to an unqualified type. (#71573)
Includes crash-reproducing test case. --------- Co-authored-by: martinboehme <[email protected]>
1 parent c9017bc commit 3001d6d

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

clang/lib/Analysis/FlowSensitive/Transfer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -683,11 +683,11 @@ class TransferVisitor : public ConstStmtVisitor<TransferVisitor> {
683683
assert(
684684
// The types are same, or
685685
Field->getType().getCanonicalType().getUnqualifiedType() ==
686-
Init->getType().getCanonicalType() ||
686+
Init->getType().getCanonicalType().getUnqualifiedType() ||
687687
// The field's type is T&, and initializer is T
688688
(Field->getType()->isReferenceType() &&
689-
Field->getType().getCanonicalType()->getPointeeType() ==
690-
Init->getType().getCanonicalType()));
689+
Field->getType().getCanonicalType()->getPointeeType() ==
690+
Init->getType().getCanonicalType()));
691691
auto& Loc = Env.createObject(Field->getType(), Init);
692692
FieldLocs.insert({Field, &Loc});
693693
}

clang/unittests/Analysis/FlowSensitive/TransferTest.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3197,6 +3197,26 @@ TEST(TransferTest, AggregateInitialization_NotExplicitlyInitializedField) {
31973197
});
31983198
}
31993199

3200+
TEST(TransferTest, AggregateInitializationFunctionPointer) {
3201+
// This is a repro for an assertion failure.
3202+
// nullptr takes on the type of a const function pointer, but its type was
3203+
// asserted to be equal to the *unqualified* type of Field, which no longer
3204+
// included the const.
3205+
std::string Code = R"(
3206+
struct S {
3207+
void (*const Field)();
3208+
};
3209+
3210+
void target() {
3211+
S s{nullptr};
3212+
}
3213+
)";
3214+
runDataflow(
3215+
Code,
3216+
[](const llvm::StringMap<DataflowAnalysisState<NoopLattice>> &Results,
3217+
ASTContext &ASTCtx) {});
3218+
}
3219+
32003220
TEST(TransferTest, AssignToUnionMember) {
32013221
std::string Code = R"(
32023222
union A {

0 commit comments

Comments
 (0)