Skip to content

Commit 5cdc525

Browse files
committed
[clang][dataflow] Fix two null pointer dereferences in getMemberForAccessor().
The additions to the test trigger crashes without the fixes.
1 parent 1328a85 commit 5cdc525

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

clang/lib/Analysis/FlowSensitive/DataflowEnvironment.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,14 @@ static void insertIfFunction(const Decl &D,
289289
}
290290

291291
static MemberExpr *getMemberForAccessor(const CXXMemberCallExpr &C) {
292+
if (!C.getMethodDecl())
293+
return nullptr;
292294
auto *Body = dyn_cast_or_null<CompoundStmt>(C.getMethodDecl()->getBody());
293295
if (!Body || Body->size() != 1)
294296
return nullptr;
295297
if (auto *RS = dyn_cast<ReturnStmt>(*Body->body_begin()))
296-
return dyn_cast<MemberExpr>(RS->getRetValue()->IgnoreParenImpCasts());
298+
if (auto *Return = RS->getRetValue())
299+
return dyn_cast<MemberExpr>(Return->IgnoreParenImpCasts());
297300
return nullptr;
298301
}
299302

clang/unittests/Analysis/FlowSensitive/TransferTest.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,6 +1463,7 @@ TEST(TransferTest, StructModeledFieldsWithAccessor) {
14631463
int getIntNotAccessed() const { return IntNotAccessed; }
14641464
int getIntNoDefinition() const;
14651465
int &getIntRef() { return IntRef; }
1466+
void returnVoid() const { return; }
14661467
};
14671468
14681469
void target() {
@@ -1473,6 +1474,14 @@ TEST(TransferTest, StructModeledFieldsWithAccessor) {
14731474
int i2 = s.getWithInc(1);
14741475
int i3 = s.getIntNoDefinition();
14751476
int &iref = s.getIntRef();
1477+
1478+
// Regression test: Don't crash on an indirect call (which doesn't have
1479+
// an associated `CXXMethodDecl`).
1480+
auto ptr_to_member_fn = &S::getPtr;
1481+
p1 = (s.*ptr_to_member_fn)();
1482+
1483+
// Regression test: Don't crash on a return statement without a value.
1484+
s.returnVoid();
14761485
// [[p]]
14771486
}
14781487
)";

0 commit comments

Comments
 (0)