Zhongxing Xu | 28a109f | 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 | |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 15 | #include "clang/Checker/PathSensitive/BugReporter.h" |
Zhongxing Xu | 28a109f | 2009-11-08 13:10:34 +0000 | [diff] [blame] | 16 | #include "clang/AST/StmtVisitor.h" |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 17 | #include "clang/Checker/LocalCheckers.h" |
Zhongxing Xu | 28a109f | 2009-11-08 13:10:34 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace clang; |
| 20 | |
| 21 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 22 | class WalkAST : public StmtVisitor<WalkAST> { |
Zhongxing Xu | 28a109f | 2009-11-08 13:10:34 +0000 | [diff] [blame] | 23 | BugReporter &BR; |
| 24 | |
| 25 | public: |
| 26 | WalkAST(BugReporter &br) : BR(br) {} |
| 27 | void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E); |
| 28 | void VisitStmt(Stmt *S) { VisitChildren(S); } |
| 29 | void VisitChildren(Stmt *S); |
| 30 | }; |
| 31 | } |
| 32 | |
| 33 | void WalkAST::VisitChildren(Stmt *S) { |
| 34 | for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I) |
| 35 | if (Stmt *child = *I) |
| 36 | Visit(child); |
| 37 | } |
| 38 | |
| 39 | // CWE-467: Use of sizeof() on a Pointer Type |
| 40 | void WalkAST::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { |
| 41 | if (!E->isSizeOf()) |
| 42 | return; |
| 43 | |
Zhongxing Xu | 52cb277 | 2009-11-10 04:20:20 +0000 | [diff] [blame] | 44 | // If an explicit type is used in the code, usually the coder knows what he is |
| 45 | // doing. |
| 46 | if (E->isArgumentType()) |
| 47 | return; |
| 48 | |
Zhongxing Xu | 28a109f | 2009-11-08 13:10:34 +0000 | [diff] [blame] | 49 | QualType T = E->getTypeOfArgument(); |
| 50 | if (T->isPointerType()) { |
Zhongxing Xu | 37e9c47 | 2009-11-10 07:52:53 +0000 | [diff] [blame] | 51 | |
| 52 | // Many false positives have the form 'sizeof *p'. This is reasonable |
| 53 | // because people know what they are doing when they intentionally |
| 54 | // dereference the pointer. |
| 55 | Expr *ArgEx = E->getArgumentExpr(); |
Zhongxing Xu | b6aa69a | 2009-11-10 08:33:44 +0000 | [diff] [blame] | 56 | if (!isa<DeclRefExpr>(ArgEx->IgnoreParens())) |
Zhongxing Xu | 37e9c47 | 2009-11-10 07:52:53 +0000 | [diff] [blame] | 57 | return; |
| 58 | |
| 59 | SourceRange R = ArgEx->getSourceRange(); |
Zhongxing Xu | 28a109f | 2009-11-08 13:10:34 +0000 | [diff] [blame] | 60 | BR.EmitBasicReport("Potential unintended use of sizeof() on pointer type", |
| 61 | "Logic", |
Zhongxing Xu | dfed7a1 | 2009-11-09 07:29:39 +0000 | [diff] [blame] | 62 | "The code calls sizeof() on a pointer type. " |
| 63 | "This can produce an unexpected result.", |
Zhongxing Xu | 28a109f | 2009-11-08 13:10:34 +0000 | [diff] [blame] | 64 | E->getLocStart(), &R, 1); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void clang::CheckSizeofPointer(const Decl *D, BugReporter &BR) { |
| 69 | WalkAST walker(BR); |
| 70 | walker.Visit(D->getBody()); |
| 71 | } |