1 //== ReturnUndefChecker.cpp -------------------------------------*- C++ -*--==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines ReturnUndefChecker, which is a path-sensitive
11 // check which looks for undefined or garbage values being returned to the
14 //===----------------------------------------------------------------------===//
16 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
17 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
18 #include "clang/StaticAnalyzer/Core/Checker.h"
19 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
23 using namespace clang;
27 class ReturnUndefChecker : public Checker< check::PreStmt<ReturnStmt> > {
28 mutable std::unique_ptr<BuiltinBug> BT_Undef;
29 mutable std::unique_ptr<BuiltinBug> BT_NullReference;
31 void emitUndef(CheckerContext &C, const Expr *RetE) const;
32 void checkReference(CheckerContext &C, const Expr *RetE,
33 DefinedOrUnknownSVal RetVal) const;
35 void checkPreStmt(const ReturnStmt *RS, CheckerContext &C) const;
39 void ReturnUndefChecker::checkPreStmt(const ReturnStmt *RS,
40 CheckerContext &C) const {
41 const Expr *RetE = RS->getRetValue();
44 SVal RetVal = C.getSVal(RetE);
46 const StackFrameContext *SFC = C.getStackFrame();
47 QualType RT = CallEvent::getDeclaredResultType(SFC->getDecl());
49 if (RetVal.isUndef()) {
50 // "return;" is modeled to evaluate to an UndefinedVal. Allow UndefinedVal
51 // to be returned in functions returning void to support this pattern:
58 if (!RT.isNull() && RT->isVoidType())
61 // Not all blocks have explicitly-specified return types; if the return type
62 // is not available, but the return value expression has 'void' type, assume
63 // Sema already checked it.
64 if (RT.isNull() && isa<BlockDecl>(SFC->getDecl()) &&
65 RetE->getType()->isVoidType())
75 if (RT->isReferenceType()) {
76 checkReference(C, RetE, RetVal.castAs<DefinedOrUnknownSVal>());
81 static void emitBug(CheckerContext &C, BuiltinBug &BT, const Expr *RetE,
82 const Expr *TrackingE = nullptr) {
83 ExplodedNode *N = C.generateErrorNode();
87 auto Report = llvm::make_unique<BugReport>(BT, BT.getDescription(), N);
89 Report->addRange(RetE->getSourceRange());
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"));
100 emitBug(C, *BT_Undef, RetE);
103 void ReturnUndefChecker::checkReference(CheckerContext &C, const Expr *RetE,
104 DefinedOrUnknownSVal RetVal) const {
105 ProgramStateRef StNonNull, StNull;
106 std::tie(StNonNull, StNull) = C.getState()->assume(RetVal);
109 // Going forward, assume the location is non-null.
110 C.addTransition(StNonNull);
114 // The return value is known to be null. Emit a bug report.
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>();