1 //=== UndefBranchChecker.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 UndefBranchChecker, which checks for undefined branch
13 //===----------------------------------------------------------------------===//
15 #include "ClangSACheckers.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/CheckerContext.h"
22 using namespace clang;
27 class UndefBranchChecker : public Checker<check::BranchCondition> {
28 mutable std::unique_ptr<BuiltinBug> BT;
30 struct FindUndefExpr {
32 const LocationContext *LCtx;
34 FindUndefExpr(ProgramStateRef S, const LocationContext *L)
35 : St(std::move(S)), LCtx(L) {}
37 const Expr *FindExpr(const Expr *Ex) {
38 if (!MatchesCriteria(Ex))
41 for (const Stmt *SubStmt : Ex->children())
42 if (const Expr *ExI = dyn_cast_or_null<Expr>(SubStmt))
43 if (const Expr *E2 = FindExpr(ExI))
49 bool MatchesCriteria(const Expr *Ex) {
50 return St->getSVal(Ex, LCtx).isUndef();
55 void checkBranchCondition(const Stmt *Condition, CheckerContext &Ctx) const;
60 void UndefBranchChecker::checkBranchCondition(const Stmt *Condition,
61 CheckerContext &Ctx) const {
62 SVal X = Ctx.getState()->getSVal(Condition, Ctx.getLocationContext());
64 // Generate a sink node, which implicitly marks both outgoing branches as
66 ExplodedNode *N = Ctx.generateErrorNode();
69 BT.reset(new BuiltinBug(
70 this, "Branch condition evaluates to a garbage value"));
72 // What's going on here: we want to highlight the subexpression of the
73 // condition that is the most likely source of the "uninitialized
74 // branch condition." We do a recursive walk of the condition's
75 // subexpressions and roughly look for the most nested subexpression
76 // that binds to Undefined. We then highlight that expression's range.
78 // Get the predecessor node and check if is a PostStmt with the Stmt
79 // being the terminator condition. We want to inspect the state
80 // of that node instead because it will contain main information about
81 // the subexpressions.
83 // Note: any predecessor will do. They should have identical state,
84 // since all the BlockEdge did was act as an error sink since the value
85 // had to already be undefined.
86 assert (!N->pred_empty());
87 const Expr *Ex = cast<Expr>(Condition);
88 ExplodedNode *PrevN = *N->pred_begin();
89 ProgramPoint P = PrevN->getLocation();
90 ProgramStateRef St = N->getState();
92 if (Optional<PostStmt> PS = P.getAs<PostStmt>())
93 if (PS->getStmt() == Ex)
94 St = PrevN->getState();
96 FindUndefExpr FindIt(St, Ctx.getLocationContext());
97 Ex = FindIt.FindExpr(Ex);
99 // Emit the bug report.
100 auto R = llvm::make_unique<BugReport>(*BT, BT->getDescription(), N);
101 bugreporter::trackNullOrUndefValue(N, Ex, *R);
102 R->addRange(Ex->getSourceRange());
104 Ctx.emitReport(std::move(R));
109 void ento::registerUndefBranchChecker(CheckerManager &mgr) {
110 mgr.registerChecker<UndefBranchChecker>();