1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" |
16 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
17 | #include "clang/StaticAnalyzer/Core/Checker.h" |
18 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
19 | #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" |
20 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
21 | |
22 | using namespace clang; |
23 | using namespace ento; |
24 | |
25 | namespace { |
26 | class ReturnUndefChecker : public Checker< check::PreStmt<ReturnStmt> > { |
27 | mutable std::unique_ptr<BuiltinBug> BT_Undef; |
28 | mutable std::unique_ptr<BuiltinBug> BT_NullReference; |
29 | |
30 | void emitUndef(CheckerContext &C, const Expr *RetE) const; |
31 | void checkReference(CheckerContext &C, const Expr *RetE, |
32 | DefinedOrUnknownSVal RetVal) const; |
33 | public: |
34 | void checkPreStmt(const ReturnStmt *RS, CheckerContext &C) const; |
35 | }; |
36 | } |
37 | |
38 | void ReturnUndefChecker::checkPreStmt(const ReturnStmt *RS, |
39 | CheckerContext &C) const { |
40 | const Expr *RetE = RS->getRetValue(); |
41 | if (!RetE) |
42 | return; |
43 | SVal RetVal = C.getSVal(RetE); |
44 | |
45 | const StackFrameContext *SFC = C.getStackFrame(); |
46 | QualType RT = CallEvent::getDeclaredResultType(SFC->getDecl()); |
47 | |
48 | if (RetVal.isUndef()) { |
49 | |
50 | |
51 | |
52 | |
53 | |
54 | |
55 | |
56 | |
57 | if (!RT.isNull() && RT->isVoidType()) |
58 | return; |
59 | |
60 | |
61 | |
62 | |
63 | if (RT.isNull() && isa<BlockDecl>(SFC->getDecl()) && |
64 | RetE->getType()->isVoidType()) |
65 | return; |
66 | |
67 | emitUndef(C, RetE); |
68 | return; |
69 | } |
70 | |
71 | if (RT.isNull()) |
72 | return; |
73 | |
74 | if (RT->isReferenceType()) { |
75 | checkReference(C, RetE, RetVal.castAs<DefinedOrUnknownSVal>()); |
76 | return; |
77 | } |
78 | } |
79 | |
80 | static void emitBug(CheckerContext &C, BuiltinBug &BT, const Expr *RetE, |
81 | const Expr *TrackingE = nullptr) { |
82 | ExplodedNode *N = C.generateErrorNode(); |
83 | if (!N) |
84 | return; |
85 | |
86 | auto Report = llvm::make_unique<BugReport>(BT, BT.getDescription(), N); |
87 | |
88 | Report->addRange(RetE->getSourceRange()); |
89 | bugreporter::trackExpressionValue(N, TrackingE ? TrackingE : RetE, *Report); |
90 | |
91 | C.emitReport(std::move(Report)); |
92 | } |
93 | |
94 | void ReturnUndefChecker::emitUndef(CheckerContext &C, const Expr *RetE) const { |
95 | if (!BT_Undef) |
96 | BT_Undef.reset( |
97 | new BuiltinBug(this, "Garbage return value", |
98 | "Undefined or garbage value returned to caller")); |
99 | emitBug(C, *BT_Undef, RetE); |
100 | } |
101 | |
102 | void ReturnUndefChecker::checkReference(CheckerContext &C, const Expr *RetE, |
103 | DefinedOrUnknownSVal RetVal) const { |
104 | ProgramStateRef StNonNull, StNull; |
105 | std::tie(StNonNull, StNull) = C.getState()->assume(RetVal); |
106 | |
107 | if (StNonNull) { |
108 | |
109 | C.addTransition(StNonNull); |
110 | return; |
111 | } |
112 | |
113 | |
114 | if (!BT_NullReference) |
115 | BT_NullReference.reset(new BuiltinBug(this, "Returning null reference")); |
116 | |
117 | emitBug(C, *BT_NullReference, RetE, bugreporter::getDerefExpr(RetE)); |
118 | } |
119 | |
120 | void ento::registerReturnUndefChecker(CheckerManager &mgr) { |
121 | mgr.registerChecker<ReturnUndefChecker>(); |
122 | } |
123 | |
124 | bool ento::shouldRegisterReturnUndefChecker(const LangOptions &LO) { |
125 | return true; |
126 | } |
127 | |