diff options
author | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2010-12-22 18:52:29 +0000 |
---|---|---|
committer | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2010-12-22 18:52:29 +0000 |
commit | 2ff5ab1516e48c2fff0138f953d887b5e695214b (patch) | |
tree | c52787595b6c148ca1a646c31f93e1eb764c362f /clang/lib/GR/CheckSizeofPointer.cpp | |
parent | 8d602a8aa8e6697509465d8a5473fc41cb1a382e (diff) | |
download | bcm5719-llvm-2ff5ab1516e48c2fff0138f953d887b5e695214b.tar.gz bcm5719-llvm-2ff5ab1516e48c2fff0138f953d887b5e695214b.zip |
[analyzer] Refactoring: lib/Checker -> lib/GR and libclangChecker -> libclangGRCore
llvm-svn: 122421
Diffstat (limited to 'clang/lib/GR/CheckSizeofPointer.cpp')
-rw-r--r-- | clang/lib/GR/CheckSizeofPointer.cpp | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/clang/lib/GR/CheckSizeofPointer.cpp b/clang/lib/GR/CheckSizeofPointer.cpp new file mode 100644 index 00000000000..4cf5eb70069 --- /dev/null +++ b/clang/lib/GR/CheckSizeofPointer.cpp @@ -0,0 +1,71 @@ +//==- CheckSizeofPointer.cpp - Check for sizeof on pointers ------*- C++ -*-==// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines a check for unintended use of sizeof() on pointer +// expressions. +// +//===----------------------------------------------------------------------===// + +#include "clang/GR/BugReporter/BugReporter.h" +#include "clang/AST/StmtVisitor.h" +#include "clang/GR/Checkers/LocalCheckers.h" + +using namespace clang; + +namespace { +class WalkAST : public StmtVisitor<WalkAST> { + BugReporter &BR; + +public: + WalkAST(BugReporter &br) : BR(br) {} + void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E); + void VisitStmt(Stmt *S) { VisitChildren(S); } + void VisitChildren(Stmt *S); +}; +} + +void WalkAST::VisitChildren(Stmt *S) { + for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I) + if (Stmt *child = *I) + Visit(child); +} + +// CWE-467: Use of sizeof() on a Pointer Type +void WalkAST::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { + if (!E->isSizeOf()) + return; + + // If an explicit type is used in the code, usually the coder knows what he is + // doing. + if (E->isArgumentType()) + return; + + QualType T = E->getTypeOfArgument(); + if (T->isPointerType()) { + + // Many false positives have the form 'sizeof *p'. This is reasonable + // because people know what they are doing when they intentionally + // dereference the pointer. + Expr *ArgEx = E->getArgumentExpr(); + if (!isa<DeclRefExpr>(ArgEx->IgnoreParens())) + return; + + SourceRange R = ArgEx->getSourceRange(); + BR.EmitBasicReport("Potential unintended use of sizeof() on pointer type", + "Logic", + "The code calls sizeof() on a pointer type. " + "This can produce an unexpected result.", + E->getLocStart(), &R, 1); + } +} + +void clang::CheckSizeofPointer(const Decl *D, BugReporter &BR) { + WalkAST walker(BR); + walker.Visit(D->getBody()); +} |