CheckSizeofPointer.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //==- CheckSizeofPointer.cpp - Check for sizeof on pointers ------*- C++ -*-==//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines a check for unintended use of sizeof() on pointer
  10. // expressions.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
  14. #include "clang/AST/StmtVisitor.h"
  15. #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
  16. #include "clang/StaticAnalyzer/Core/Checker.h"
  17. #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
  18. using namespace clang;
  19. using namespace ento;
  20. namespace {
  21. class WalkAST : public StmtVisitor<WalkAST> {
  22. BugReporter &BR;
  23. const CheckerBase *Checker;
  24. AnalysisDeclContext* AC;
  25. public:
  26. WalkAST(BugReporter &br, const CheckerBase *checker, AnalysisDeclContext *ac)
  27. : BR(br), Checker(checker), AC(ac) {}
  28. void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
  29. void VisitStmt(Stmt *S) { VisitChildren(S); }
  30. void VisitChildren(Stmt *S);
  31. };
  32. }
  33. void WalkAST::VisitChildren(Stmt *S) {
  34. for (Stmt *Child : S->children())
  35. if (Child)
  36. Visit(Child);
  37. }
  38. // CWE-467: Use of sizeof() on a Pointer Type
  39. void WalkAST::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
  40. if (E->getKind() != UETT_SizeOf)
  41. return;
  42. // If an explicit type is used in the code, usually the coder knows what they are
  43. // doing.
  44. if (E->isArgumentType())
  45. return;
  46. QualType T = E->getTypeOfArgument();
  47. if (T->isPointerType()) {
  48. // Many false positives have the form 'sizeof *p'. This is reasonable
  49. // because people know what they are doing when they intentionally
  50. // dereference the pointer.
  51. Expr *ArgEx = E->getArgumentExpr();
  52. if (!isa<DeclRefExpr>(ArgEx->IgnoreParens()))
  53. return;
  54. PathDiagnosticLocation ELoc =
  55. PathDiagnosticLocation::createBegin(E, BR.getSourceManager(), AC);
  56. BR.EmitBasicReport(AC->getDecl(), Checker,
  57. "Potential unintended use of sizeof() on pointer type",
  58. categories::LogicError,
  59. "The code calls sizeof() on a pointer type. "
  60. "This can produce an unexpected result.",
  61. ELoc, ArgEx->getSourceRange());
  62. }
  63. }
  64. //===----------------------------------------------------------------------===//
  65. // SizeofPointerChecker
  66. //===----------------------------------------------------------------------===//
  67. namespace {
  68. class SizeofPointerChecker : public Checker<check::ASTCodeBody> {
  69. public:
  70. void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
  71. BugReporter &BR) const {
  72. WalkAST walker(BR, this, mgr.getAnalysisDeclContext(D));
  73. walker.Visit(D->getBody());
  74. }
  75. };
  76. }
  77. void ento::registerSizeofPointerChecker(CheckerManager &mgr) {
  78. mgr.registerChecker<SizeofPointerChecker>();
  79. }
  80. bool ento::shouldRegisterSizeofPointerChecker(const CheckerManager &mgr) {
  81. return true;
  82. }