clang  3.9.0
UndefResultChecker.cpp
Go to the documentation of this file.
1 //=== UndefResultChecker.cpp ------------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This defines UndefResultChecker, a builtin check in ExprEngine that
11 // performs checks for undefined results of non-assignment binary operators.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "ClangSACheckers.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/Support/raw_ostream.h"
23 
24 using namespace clang;
25 using namespace ento;
26 
27 namespace {
28 class UndefResultChecker
29  : public Checker< check::PostStmt<BinaryOperator> > {
30 
31  mutable std::unique_ptr<BugType> BT;
32 
33 public:
34  void checkPostStmt(const BinaryOperator *B, CheckerContext &C) const;
35 };
36 } // end anonymous namespace
37 
38 void UndefResultChecker::checkPostStmt(const BinaryOperator *B,
39  CheckerContext &C) const {
41  const LocationContext *LCtx = C.getLocationContext();
42  if (state->getSVal(B, LCtx).isUndef()) {
43 
44  // Do not report assignments of uninitialized values inside swap functions.
45  // This should allow to swap partially uninitialized structs
46  // (radar://14129997)
47  if (const FunctionDecl *EnclosingFunctionDecl =
48  dyn_cast<FunctionDecl>(C.getStackFrame()->getDecl()))
49  if (C.getCalleeName(EnclosingFunctionDecl) == "swap")
50  return;
51 
52  // Generate an error node.
54  if (!N)
55  return;
56 
57  if (!BT)
58  BT.reset(
59  new BuiltinBug(this, "Result of operation is garbage or undefined"));
60 
61  SmallString<256> sbuf;
62  llvm::raw_svector_ostream OS(sbuf);
63  const Expr *Ex = nullptr;
64  bool isLeft = true;
65 
66  if (state->getSVal(B->getLHS(), LCtx).isUndef()) {
67  Ex = B->getLHS()->IgnoreParenCasts();
68  isLeft = true;
69  }
70  else if (state->getSVal(B->getRHS(), LCtx).isUndef()) {
71  Ex = B->getRHS()->IgnoreParenCasts();
72  isLeft = false;
73  }
74 
75  if (Ex) {
76  OS << "The " << (isLeft ? "left" : "right")
77  << " operand of '"
79  << "' is a garbage value";
80  }
81  else {
82  // Neither operand was undefined, but the result is undefined.
83  OS << "The result of the '"
85  << "' expression is undefined";
86  }
87  auto report = llvm::make_unique<BugReport>(*BT, OS.str(), N);
88  if (Ex) {
89  report->addRange(Ex->getSourceRange());
90  bugreporter::trackNullOrUndefValue(N, Ex, *report);
91  }
92  else
94 
95  C.emitReport(std::move(report));
96  }
97 }
98 
99 void ento::registerUndefResultChecker(CheckerManager &mgr) {
100  mgr.registerChecker<UndefResultChecker>();
101 }
FunctionDecl - An instance of this class is created to represent a function declaration or definition...
Definition: Decl.h:1561
StringRef getCalleeName(const FunctionDecl *FunDecl) const
Get the name of the called function (path-sensitive).
ExplodedNode * generateErrorNode(ProgramStateRef State=nullptr, const ProgramPointTag *Tag=nullptr)
Generate a transition to a node that will be used to report an error.
i32 captured_struct **param SharedsTy A type which contains references the shared variables *param Shareds Context with the list of shared variables from the p *TaskFunction *param Data Additional data for task generation like final * state
Expr * getLHS() const
Definition: Expr.h:2943
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:2897
Expr * IgnoreParenCasts() LLVM_READONLY
IgnoreParenCasts - Ignore parentheses and casts.
Definition: Expr.cpp:2326
Expr - This represents one expression.
Definition: Expr.h:105
const ProgramStateRef & getState() const
void emitReport(std::unique_ptr< BugReport > R)
Emit the diagnostics report.
CHECKER * registerChecker()
Used to register checkers.
const Decl * getDecl() const
const StackFrameContext * getStackFrame() const
StringRef getOpcodeStr() const
Definition: Expr.h:2959
Opcode getOpcode() const
Definition: Expr.h:2940
bool trackNullOrUndefValue(const ExplodedNode *N, const Stmt *S, BugReport &R, bool IsArg=false, bool EnableNullFPSuppression=true)
Attempts to add visitors to trace a null or undefined value back to its point of origin, whether it is a symbol constrained to null or an explicit assignment.
Expr * getRHS() const
Definition: Expr.h:2945
const LocationContext * getLocationContext() const