Skip to content

Commit

Permalink
In VirtualCallChecker, handle indirect calls
Browse files Browse the repository at this point in the history
Summary:
In VirtualCallChecker, handle indirect calls.

getDirectCallee() can be nullptr, and dyn_cast(nullptr) is UB

Reviewers: bkramer

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D29303

llvm-svn: 293604
  • Loading branch information
sam-mccall committed Jan 31, 2017
1 parent 06e038c commit 93590e0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion clang/lib/StaticAnalyzer/Checkers/VirtualCallChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ void WalkAST::VisitCXXMemberCallExpr(CallExpr *CE) {
}

// Get the callee.
const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CE->getDirectCallee());
const CXXMethodDecl *MD =
dyn_cast_or_null<CXXMethodDecl>(CE->getDirectCallee());
if (MD && MD->isVirtual() && !callIsNonVirtual && !MD->hasAttr<FinalAttr>() &&
!MD->getParent()->hasAttr<FinalAttr>())
ReportVirtualCall(CE, MD->isPure());
Expand Down
11 changes: 11 additions & 0 deletions clang/test/Analysis/virtualcall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,23 @@ class E final : public B {
int foo() override;
};

// Regression test: don't crash when there's no direct callee.
class F {
public:
F() {
void (F::* ptr)() = &F::foo;
(this->*ptr)();
}
void foo();
};

int main() {
A *a;
B *b;
C *c;
D *d;
E *e;
F *f;
}

#include "virtualcall.h"
Expand Down

0 comments on commit 93590e0

Please sign in to comment.