22 using namespace clang;
26 class ReturnUndefChecker :
public Checker< check::PreStmt<ReturnStmt> > {
27 mutable std::unique_ptr<BuiltinBug> BT_Undef;
28 mutable std::unique_ptr<BuiltinBug> BT_NullReference;
30 void emitUndef(CheckerContext &C,
const Expr *RetE)
const;
31 void checkReference(CheckerContext &C,
const Expr *RetE,
32 DefinedOrUnknownSVal RetVal)
const;
34 void checkPreStmt(
const ReturnStmt *RS, CheckerContext &C)
const;
38 void ReturnUndefChecker::checkPreStmt(
const ReturnStmt *RS,
39 CheckerContext &C)
const {
43 SVal RetVal = C.getSVal(RetE);
48 if (RetVal.isUndef()) {
75 checkReference(C, RetE, RetVal.castAs<DefinedOrUnknownSVal>());
80 static void emitBug(CheckerContext &C, BuiltinBug &BT,
const Expr *RetE,
81 const Expr *TrackingE =
nullptr) {
82 ExplodedNode *N = C.generateErrorNode();
86 auto Report = llvm::make_unique<BugReport>(BT, BT.getDescription(), N);
89 bugreporter::trackExpressionValue(N, TrackingE ? TrackingE : RetE, *Report);
91 C.emitReport(std::move(Report));
94 void ReturnUndefChecker::emitUndef(CheckerContext &C,
const Expr *RetE)
const {
97 new BuiltinBug(
this,
"Garbage return value",
98 "Undefined or garbage value returned to caller"));
102 void ReturnUndefChecker::checkReference(CheckerContext &C,
const Expr *RetE,
103 DefinedOrUnknownSVal RetVal)
const {
105 std::tie(StNonNull, StNull) = C.getState()->assume(RetVal);
109 C.addTransition(StNonNull);
114 if (!BT_NullReference)
115 BT_NullReference.reset(
new BuiltinBug(
this,
"Returning null reference"));
117 emitBug(C, *BT_NullReference, RetE, bugreporter::getDerefExpr(RetE));
120 void ento::registerReturnUndefChecker(CheckerManager &mgr) {
121 mgr.registerChecker<ReturnUndefChecker>();
124 bool ento::shouldRegisterReturnUndefChecker(
const LangOptions &LO) {
A (possibly-)qualified type.
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
static void emitBug(CheckerContext &C, BuiltinBug &BT, const Expr *RetE, const Expr *TrackingE=nullptr)
bool isReferenceType() const
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
This represents one expression.
ReturnStmt - This represents a return, optionally of an expression: return; return 4;...
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Dataflow Directional Tag Classes.
const Decl * getDecl() const
static QualType getDeclaredResultType(const Decl *D)
Returns the result type of a function or method declaration.
const StackFrameContext * getStackFrame() const
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...