Zhongxing Xu | b0a05f7c | 2009-11-08 13:10:34 +0000 | [diff] [blame] | 1 | //==- CheckSizeofPointer.cpp - Check for sizeof on pointers ------*- 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 a check for unintended use of sizeof() on pointer |
| 11 | // expressions. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Argyrios Kyrtzidis | af45aca | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 15 | #include "ClangSACheckers.h" |
Anna Zaks | c29bed3 | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 16 | #include "clang/AST/StmtVisitor.h" |
Ted Kremenek | f8cbac4 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 17 | #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Anna Zaks | c29bed3 | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 19 | #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h" |
Zhongxing Xu | b0a05f7c | 2009-11-08 13:10:34 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace clang; |
Ted Kremenek | 98857c9 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 22 | using namespace ento; |
Zhongxing Xu | b0a05f7c | 2009-11-08 13:10:34 +0000 | [diff] [blame] | 23 | |
| 24 | namespace { |
Kovarththanan Rajaratnam | 65c6566 | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 25 | class WalkAST : public StmtVisitor<WalkAST> { |
Zhongxing Xu | b0a05f7c | 2009-11-08 13:10:34 +0000 | [diff] [blame] | 26 | BugReporter &BR; |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame^] | 27 | const CheckerBase *Checker; |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 28 | AnalysisDeclContext* AC; |
Zhongxing Xu | b0a05f7c | 2009-11-08 13:10:34 +0000 | [diff] [blame] | 29 | |
| 30 | public: |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame^] | 31 | WalkAST(BugReporter &br, const CheckerBase *checker, AnalysisDeclContext *ac) |
| 32 | : BR(br), Checker(checker), AC(ac) {} |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 33 | void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E); |
Zhongxing Xu | b0a05f7c | 2009-11-08 13:10:34 +0000 | [diff] [blame] | 34 | void VisitStmt(Stmt *S) { VisitChildren(S); } |
| 35 | void VisitChildren(Stmt *S); |
| 36 | }; |
| 37 | } |
| 38 | |
| 39 | void WalkAST::VisitChildren(Stmt *S) { |
| 40 | for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I) |
| 41 | if (Stmt *child = *I) |
| 42 | Visit(child); |
| 43 | } |
| 44 | |
| 45 | // CWE-467: Use of sizeof() on a Pointer Type |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 46 | void WalkAST::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) { |
| 47 | if (E->getKind() != UETT_SizeOf) |
Zhongxing Xu | b0a05f7c | 2009-11-08 13:10:34 +0000 | [diff] [blame] | 48 | return; |
| 49 | |
Zhongxing Xu | 9a7448c | 2009-11-10 04:20:20 +0000 | [diff] [blame] | 50 | // If an explicit type is used in the code, usually the coder knows what he is |
| 51 | // doing. |
| 52 | if (E->isArgumentType()) |
| 53 | return; |
| 54 | |
Zhongxing Xu | b0a05f7c | 2009-11-08 13:10:34 +0000 | [diff] [blame] | 55 | QualType T = E->getTypeOfArgument(); |
| 56 | if (T->isPointerType()) { |
Zhongxing Xu | 537db5d | 2009-11-10 07:52:53 +0000 | [diff] [blame] | 57 | |
| 58 | // Many false positives have the form 'sizeof *p'. This is reasonable |
| 59 | // because people know what they are doing when they intentionally |
| 60 | // dereference the pointer. |
| 61 | Expr *ArgEx = E->getArgumentExpr(); |
Zhongxing Xu | f966722 | 2009-11-10 08:33:44 +0000 | [diff] [blame] | 62 | if (!isa<DeclRefExpr>(ArgEx->IgnoreParens())) |
Zhongxing Xu | 537db5d | 2009-11-10 07:52:53 +0000 | [diff] [blame] | 63 | return; |
| 64 | |
Anna Zaks | c29bed3 | 2011-09-20 21:38:35 +0000 | [diff] [blame] | 65 | PathDiagnosticLocation ELoc = |
| 66 | PathDiagnosticLocation::createBegin(E, BR.getSourceManager(), AC); |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame^] | 67 | BR.EmitBasicReport(AC->getDecl(), Checker, |
Ted Kremenek | 5a10f08 | 2012-04-04 18:11:35 +0000 | [diff] [blame] | 68 | "Potential unintended use of sizeof() on pointer type", |
Jordan Rose | 6feda28 | 2013-10-04 00:25:24 +0000 | [diff] [blame] | 69 | categories::LogicError, |
Zhongxing Xu | 8500020 | 2009-11-09 07:29:39 +0000 | [diff] [blame] | 70 | "The code calls sizeof() on a pointer type. " |
| 71 | "This can produce an unexpected result.", |
Jordan Rose | 42b4248 | 2013-10-07 17:16:59 +0000 | [diff] [blame] | 72 | ELoc, ArgEx->getSourceRange()); |
Zhongxing Xu | b0a05f7c | 2009-11-08 13:10:34 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
Argyrios Kyrtzidis | af45aca | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 76 | //===----------------------------------------------------------------------===// |
| 77 | // SizeofPointerChecker |
| 78 | //===----------------------------------------------------------------------===// |
| 79 | |
| 80 | namespace { |
Argyrios Kyrtzidis | 6a5674f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 81 | class SizeofPointerChecker : public Checker<check::ASTCodeBody> { |
Argyrios Kyrtzidis | af45aca | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 82 | public: |
| 83 | void checkASTCodeBody(const Decl *D, AnalysisManager& mgr, |
| 84 | BugReporter &BR) const { |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame^] | 85 | WalkAST walker(BR, this, mgr.getAnalysisDeclContext(D)); |
Argyrios Kyrtzidis | af45aca | 2011-02-17 21:39:33 +0000 | [diff] [blame] | 86 | walker.Visit(D->getBody()); |
| 87 | } |
| 88 | }; |
| 89 | } |
| 90 | |
| 91 | void ento::registerSizeofPointerChecker(CheckerManager &mgr) { |
| 92 | mgr.registerChecker<SizeofPointerChecker>(); |
Zhongxing Xu | b0a05f7c | 2009-11-08 13:10:34 +0000 | [diff] [blame] | 93 | } |