clang 24.0.0git
UseAfterLifetimeEnd.cpp
Go to the documentation of this file.
1#include "LifetimeModeling.h"
4
5using namespace clang;
6using namespace ento;
7
8namespace {
9class UseAfterLifetimeEnd : public Checker<check::EndFunction> {
10public:
11 void reportDanglingSource(const MemRegion *Source, ExplodedNode *N,
12 CheckerContext &C) const;
13 void checkEndFunction(const ReturnStmt *RS, CheckerContext &C) const;
14 const BugType BugMsg{this, "UseAfterLifetimeEnd", "LifetimeBound"};
15};
16
17} // namespace
18
19void UseAfterLifetimeEnd::checkEndFunction(const ReturnStmt *RS,
20 CheckerContext &C) const {
21 if (!RS)
22 return;
23
24 ProgramStateRef State = C.getState();
25
26 const Expr *RetExpr = RS->getRetValue();
27 if (!RetExpr)
28 return;
29
30 RetExpr = RetExpr->IgnoreParens();
31 SVal RetVal = C.getSVal(RetExpr);
32
33 std::vector<const MemRegion *> RetValRegion =
35 if (RetValRegion.empty())
36 return;
37
38 if (ExplodedNode *N =
39 C.generateNonFatalErrorNode(State, C.getPredecessor())) {
40 for (const MemRegion *R : RetValRegion)
41 reportDanglingSource(R, N, C);
42 }
43}
44
45void UseAfterLifetimeEnd::reportDanglingSource(const MemRegion *Source,
46 ExplodedNode *N,
47 CheckerContext &C) const {
48 auto BR = std::make_unique<PathSensitiveBugReport>(
49 BugMsg,
50 (llvm::Twine("Returning value bound to '") + Source->getString() +
51 "' that will go out of scope"),
52 N);
53 C.emitReport(std::move(BR));
54}
55
56void ento::registerUseAfterLifetimeEnd(CheckerManager &Mgr) {
57 Mgr.registerChecker<UseAfterLifetimeEnd>();
58}
59
60bool ento::shouldRegisterUseAfterLifetimeEnd(const CheckerManager &Mgr) {
61 return true;
62}
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3095
ReturnStmt - This represents a return, optionally of an expression: return; return 4;.
Definition Stmt.h:3170
Expr * getRetValue()
Definition Stmt.h:3197
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.
Definition Checker.h:565
std::string getString() const
Get a string representation of a region for debug use.
std::vector< const MemRegion * > getDanglingRegionsAfterReturn(SVal Source, ProgramStateRef State, CheckerContext &C)
Returns the set of lifetime sources bound to Source that are dangling stack regions.
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
The JSON file list parser is used to communicate input to InstallAPI.