ReturnUndefChecker.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. //== ReturnUndefChecker.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 file defines ReturnUndefChecker, which is a path-sensitive
  11. // check which looks for undefined or garbage values being returned to the
  12. // caller.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "InternalChecks.h"
  16. #include "clang/StaticAnalyzer/BugReporter/BugType.h"
  17. #include "clang/StaticAnalyzer/PathSensitive/CheckerVisitor.h"
  18. #include "clang/StaticAnalyzer/PathSensitive/ExprEngine.h"
  19. using namespace clang;
  20. using namespace ento;
  21. namespace {
  22. class ReturnUndefChecker :
  23. public CheckerVisitor<ReturnUndefChecker> {
  24. BuiltinBug *BT;
  25. public:
  26. ReturnUndefChecker() : BT(0) {}
  27. static void *getTag();
  28. void PreVisitReturnStmt(CheckerContext &C, const ReturnStmt *RS);
  29. };
  30. }
  31. void ento::RegisterReturnUndefChecker(ExprEngine &Eng) {
  32. Eng.registerCheck(new ReturnUndefChecker());
  33. }
  34. void *ReturnUndefChecker::getTag() {
  35. static int x = 0; return &x;
  36. }
  37. void ReturnUndefChecker::PreVisitReturnStmt(CheckerContext &C,
  38. const ReturnStmt *RS) {
  39. const Expr *RetE = RS->getRetValue();
  40. if (!RetE)
  41. return;
  42. if (!C.getState()->getSVal(RetE).isUndef())
  43. return;
  44. ExplodedNode *N = C.generateSink();
  45. if (!N)
  46. return;
  47. if (!BT)
  48. BT = new BuiltinBug("Garbage return value",
  49. "Undefined or garbage value returned to caller");
  50. EnhancedBugReport *report =
  51. new EnhancedBugReport(*BT, BT->getDescription(), N);
  52. report->addRange(RetE->getSourceRange());
  53. report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, RetE);
  54. C.EmitReport(report);
  55. }