12class DanglingPtrDeref :
public Checker<check::Location> {
14 void checkLocation(SVal Loc,
bool IsLoad,
const Stmt *S,
15 CheckerContext &
C)
const;
16 void reportUseAfterScope(
const MemRegion *Region, ExplodedNode *N,
17 CheckerContext &
C)
const;
18 const BugType BugMsg{
this,
"ReportDanglingPtrDeref",
"LifetimeBound"};
22 const MemRegion *SourceRegion;
25 explicit DanglingPtrDerefBRVisitor(
const MemRegion *Source)
26 : SourceRegion(Source) {}
28 void Profile(llvm::FoldingSetNodeID &ID)
const override {
29 ID.AddPointer(SourceRegion);
33 BugReporterContext &BRC,
34 PathSensitiveBugReport &BR)
override;
39void DanglingPtrDeref::checkLocation(
SVal Loc,
bool IsLoad,
const Stmt *S,
43 if (
const MemRegion *LocRegion = Loc.
getAsRegion()) {
45 if (ExplodedNode *N =
C.generateNonFatalErrorNode(State))
46 reportUseAfterScope(LocRegion, N,
C);
51void DanglingPtrDeref::reportUseAfterScope(
const MemRegion *Region,
53 CheckerContext &
C)
const {
54 auto BR = std::make_unique<PathSensitiveBugReport>(
56 (llvm::Twine(
"Use of '") + Region->
getString() +
57 "' after its lifetime ended."),
59 BR->addVisitor<DanglingPtrDerefBRVisitor>(Region);
60 C.emitReport(std::move(BR));
64DanglingPtrDerefBRVisitor::VisitNode(
const ExplodedNode *N,
65 BugReporterContext &BRC,
66 PathSensitiveBugReport &BR) {
82 return std::make_shared<PathDiagnosticEventPiece>(
84 (llvm::Twine(
"'") + SourceRegion->
getString() +
"' is destroyed here")
89void ento::registerDanglingPtrDeref(CheckerManager &Mgr) {
93bool ento::shouldRegisterDanglingPtrDeref(
const CheckerManager &Mgr) {
Stmt - This represents one statement.
const SourceManager & getSourceManager() const
BugReporterVisitors are used to add custom diagnostics along a path.
CHECKER * registerChecker(AT &&...Args)
Register a single-part checker (derived from Checker): construct its singleton instance,...
Simple checker classes that implement one frontend (i.e.
const ProgramStateRef & getState() const
const Stmt * getStmtForDiagnostics() const
If the node's program point corresponds to a statement, retrieve that statement.
ExplodedNode * getFirstPred()
const StackFrame * getStackFrame() const
std::string getString() const
Get a string representation of a region for debug use.
static PathDiagnosticLocation createEnd(const Stmt *S, const SourceManager &SM, const StackFrameOrAnalysisDeclContext SFAC)
Create a location for the end of the statement.
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
const MemRegion * getAsRegion() const
bool isDeallocated(ProgramStateRef State, const MemRegion *Region)
Returns true if the underlying MemRegion is deallocated.
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
std::shared_ptr< PathDiagnosticPiece > PathDiagnosticPieceRef
The JSON file list parser is used to communicate input to InstallAPI.