22 using namespace clang;
28 class InvalidatedIteratorChecker
29 :
public Checker<check::PreCall> {
31 std::unique_ptr<BugType> InvalidatedBugType;
33 void verifyAccess(CheckerContext &C,
const SVal &Val)
const;
34 void reportBug(
const StringRef &Message,
const SVal &Val,
35 CheckerContext &C, ExplodedNode *ErrNode)
const;
37 InvalidatedIteratorChecker();
39 void checkPreCall(
const CallEvent &Call, CheckerContext &C)
const;
45 InvalidatedIteratorChecker::InvalidatedIteratorChecker() {
46 InvalidatedBugType.reset(
47 new BugType(
this,
"Iterator invalidated",
"Misuse of STL APIs"));
50 void InvalidatedIteratorChecker::checkPreCall(
const CallEvent &Call,
51 CheckerContext &C)
const {
53 const auto *Func = dyn_cast_or_null<FunctionDecl>(Call.getDecl());
57 if (Func->isOverloadedOperator() &&
60 if (
const auto *InstCall = dyn_cast<CXXInstanceCall>(&Call)) {
61 verifyAccess(C, InstCall->getCXXThisVal());
63 verifyAccess(C, Call.getArgSVal(0));
68 void InvalidatedIteratorChecker::verifyAccess(CheckerContext &C,
const SVal &Val)
const {
69 auto State = C.getState();
71 if (Pos && !Pos->isValid()) {
72 auto *N = C.generateErrorNode(
State);
76 reportBug(
"Invalidated iterator accessed.", Val, C, N);
80 void InvalidatedIteratorChecker::reportBug(
const StringRef &Message,
81 const SVal &Val, CheckerContext &C,
82 ExplodedNode *ErrNode)
const {
83 auto R = std::make_unique<PathSensitiveBugReport>(*InvalidatedBugType,
85 R->markInteresting(Val);
86 C.emitReport(std::move(R));
89 void ento::registerInvalidatedIteratorChecker(CheckerManager &mgr) {
90 mgr.registerChecker<InvalidatedIteratorChecker>();
93 bool ento::shouldRegisterInvalidatedIteratorChecker(
const LangOptions &LO) {
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
const IteratorPosition * getIteratorPosition(ProgramStateRef State, const SVal &Val)
bool isAccessOperator(OverloadedOperatorKind OK)
Dataflow Directional Tag Classes.