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();
87 std::make_unique<PathSensitiveBugReport>(BT, BT.getDescription(), N);
90 bugreporter::trackExpressionValue(N, TrackingE ? TrackingE : RetE, *Report);
92 C.emitReport(std::move(Report));
95 void ReturnUndefChecker::emitUndef(CheckerContext &C,
const Expr *RetE)
const {
98 new BuiltinBug(
this,
"Garbage return value",
99 "Undefined or garbage value returned to caller"));
103 void ReturnUndefChecker::checkReference(CheckerContext &C,
const Expr *RetE,
104 DefinedOrUnknownSVal RetVal)
const {
106 std::tie(StNonNull, StNull) = C.getState()->assume(RetVal);
110 C.addTransition(StNonNull);
115 if (!BT_NullReference)
116 BT_NullReference.reset(
new BuiltinBug(
this,
"Returning null reference"));
118 emitBug(C, *BT_NullReference, RetE, bugreporter::getDerefExpr(RetE));
121 void ento::registerReturnUndefChecker(CheckerManager &mgr) {
122 mgr.registerChecker<ReturnUndefChecker>();
125 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...