UndefinedAssignmentChecker.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //===--- UndefinedAssignmentChecker.h ---------------------------*- 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 UndefinedAssignmentChecker, a builtin check in ExprEngine that
  11. // checks for assigning undefined values.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "ClangSACheckers.h"
  15. #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
  16. #include "clang/StaticAnalyzer/Core/Checker.h"
  17. #include "clang/StaticAnalyzer/Core/CheckerManager.h"
  18. #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
  19. using namespace clang;
  20. using namespace ento;
  21. namespace {
  22. class UndefinedAssignmentChecker
  23. : public Checker<check::Bind> {
  24. mutable std::unique_ptr<BugType> BT;
  25. public:
  26. void checkBind(SVal location, SVal val, const Stmt *S,
  27. CheckerContext &C) const;
  28. };
  29. }
  30. void UndefinedAssignmentChecker::checkBind(SVal location, SVal val,
  31. const Stmt *StoreE,
  32. CheckerContext &C) const {
  33. if (!val.isUndef())
  34. return;
  35. // Do not report assignments of uninitialized values inside swap functions.
  36. // This should allow to swap partially uninitialized structs
  37. // (radar://14129997)
  38. if (const FunctionDecl *EnclosingFunctionDecl =
  39. dyn_cast<FunctionDecl>(C.getStackFrame()->getDecl()))
  40. if (C.getCalleeName(EnclosingFunctionDecl) == "swap")
  41. return;
  42. ExplodedNode *N = C.generateErrorNode();
  43. if (!N)
  44. return;
  45. const char *str = "Assigned value is garbage or undefined";
  46. if (!BT)
  47. BT.reset(new BuiltinBug(this, str));
  48. // Generate a report for this bug.
  49. const Expr *ex = nullptr;
  50. while (StoreE) {
  51. if (const UnaryOperator *U = dyn_cast<UnaryOperator>(StoreE)) {
  52. str = "The expression is an uninitialized value. "
  53. "The computed value will also be garbage";
  54. ex = U->getSubExpr();
  55. break;
  56. }
  57. if (const BinaryOperator *B = dyn_cast<BinaryOperator>(StoreE)) {
  58. if (B->isCompoundAssignmentOp()) {
  59. if (C.getSVal(B->getLHS()).isUndef()) {
  60. str = "The left expression of the compound assignment is an "
  61. "uninitialized value. The computed value will also be garbage";
  62. ex = B->getLHS();
  63. break;
  64. }
  65. }
  66. ex = B->getRHS();
  67. break;
  68. }
  69. if (const DeclStmt *DS = dyn_cast<DeclStmt>(StoreE)) {
  70. const VarDecl *VD = dyn_cast<VarDecl>(DS->getSingleDecl());
  71. ex = VD->getInit();
  72. }
  73. break;
  74. }
  75. auto R = llvm::make_unique<BugReport>(*BT, str, N);
  76. if (ex) {
  77. R->addRange(ex->getSourceRange());
  78. bugreporter::trackNullOrUndefValue(N, ex, *R);
  79. }
  80. C.emitReport(std::move(R));
  81. }
  82. void ento::registerUndefinedAssignmentChecker(CheckerManager &mgr) {
  83. mgr.registerChecker<UndefinedAssignmentChecker>();
  84. }