Skip to content

Commit

Permalink
[analyzer] MacOSKeychainAPIChecker: Add the custom BugReport visitor(…
Browse files Browse the repository at this point in the history
…which highlights the allocation site) to all the relevant reports within the checker.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@138531 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
AnnaZaks committed Aug 25, 2011
1 parent b6cfc09 commit 6b7aad9
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions lib/StaticAnalyzer/Checkers/MacOSKeychainAPIChecker.cpp
Expand Up @@ -97,10 +97,9 @@ class MacOSKeychainAPIChecker : public Checker<check::PreStmt<CallExpr>,
BT.reset(new BugType("Improper use of SecKeychain API", "Mac OS API"));
}

void generateDeallocatorMismatchReport(const AllocationState &AS,
void generateDeallocatorMismatchReport(const AllocationPair &AP,
const Expr *ArgExpr,
CheckerContext &C,
SymbolRef ArgSM) const;
CheckerContext &C) const;

BugReport *generateAllocatedDataNotReleasedReport(const AllocationPair &AP,
ExplodedNode *N) const;
Expand Down Expand Up @@ -260,24 +259,25 @@ bool MacOSKeychainAPIChecker::definitelyReturnedError(SymbolRef RetSym,
// Report deallocator mismatch. Remove the region from tracking - reporting a
// missing free error after this one is redundant.
void MacOSKeychainAPIChecker::
generateDeallocatorMismatchReport(const AllocationState &AS,
generateDeallocatorMismatchReport(const AllocationPair &AP,
const Expr *ArgExpr,
CheckerContext &C,
SymbolRef ArgSM) const {
CheckerContext &C) const {
const ProgramState *State = C.getState();
State = State->remove<AllocatedData>(ArgSM);
State = State->remove<AllocatedData>(AP.first);
ExplodedNode *N = C.generateNode(State);

if (!N)
return;
initBugType();
llvm::SmallString<80> sbuf;
llvm::raw_svector_ostream os(sbuf);
unsigned int PDeallocIdx = FunctionsToTrack[AS.AllocatorIdx].DeallocatorIdx;
unsigned int PDeallocIdx =
FunctionsToTrack[AP.second->AllocatorIdx].DeallocatorIdx;

os << "Deallocator doesn't match the allocator: '"
<< FunctionsToTrack[PDeallocIdx].Name << "' should be used.";
BugReport *Report = new BugReport(*BT, os.str(), N);
Report->addVisitor(new SecKeychainBugVisitor(AP.first));
Report->addRange(ArgExpr->getSourceRange());
C.EmitReport(Report);
}
Expand Down Expand Up @@ -319,6 +319,7 @@ void MacOSKeychainAPIChecker::checkPreStmt(const CallExpr *CE,
<< FunctionsToTrack[DIdx].Name
<< "'.";
BugReport *Report = new BugReport(*BT, os.str(), N);
Report->addVisitor(new SecKeychainBugVisitor(V));
Report->addRange(ArgExpr->getSourceRange());
C.EmitReport(Report);
}
Expand Down Expand Up @@ -383,7 +384,8 @@ void MacOSKeychainAPIChecker::checkPreStmt(const CallExpr *CE,
// NULL ~ default deallocator, so warn.
if (DeallocatorExpr->isNullPointerConstant(C.getASTContext(),
Expr::NPC_ValueDependentIsNotNull)) {
generateDeallocatorMismatchReport(*AS, ArgExpr, C, ArgSM);
const AllocationPair AP = std::make_pair(ArgSM, AS);
generateDeallocatorMismatchReport(AP, ArgExpr, C);
return;
}
// One of the default allocators, so warn.
Expand All @@ -392,7 +394,8 @@ void MacOSKeychainAPIChecker::checkPreStmt(const CallExpr *CE,
if (DeallocatorName == "kCFAllocatorDefault" ||
DeallocatorName == "kCFAllocatorSystemDefault" ||
DeallocatorName == "kCFAllocatorMalloc") {
generateDeallocatorMismatchReport(*AS, ArgExpr, C, ArgSM);
const AllocationPair AP = std::make_pair(ArgSM, AS);
generateDeallocatorMismatchReport(AP, ArgExpr, C);
return;
}
// If kCFAllocatorNull, which does not deallocate, we still have to
Expand All @@ -415,7 +418,8 @@ void MacOSKeychainAPIChecker::checkPreStmt(const CallExpr *CE,
// Check if the proper deallocator is used.
unsigned int PDeallocIdx = FunctionsToTrack[AS->AllocatorIdx].DeallocatorIdx;
if (PDeallocIdx != idx || (FunctionsToTrack[idx].Kind == ErrorAPI)) {
generateDeallocatorMismatchReport(*AS, ArgExpr, C, ArgSM);
const AllocationPair AP = std::make_pair(ArgSM, AS);
generateDeallocatorMismatchReport(AP, ArgExpr, C);
return;
}

Expand All @@ -427,6 +431,7 @@ void MacOSKeychainAPIChecker::checkPreStmt(const CallExpr *CE,
initBugType();
BugReport *Report = new BugReport(*BT,
"Call to free data when error was returned during allocation.", N);
Report->addVisitor(new SecKeychainBugVisitor(ArgSM));
Report->addRange(ArgExpr->getSourceRange());
C.EmitReport(Report);
return;
Expand Down

0 comments on commit 6b7aad9

Please sign in to comment.