2009-11-08 21:10:34 +08:00
|
|
|
//==- 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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-02-18 05:39:33 +08:00
|
|
|
#include "ClangSACheckers.h"
|
2011-09-21 05:38:35 +08:00
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2011-02-10 09:03:03 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
2011-09-21 05:38:35 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
|
2009-11-08 21:10:34 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2009-11-08 21:10:34 +08:00
|
|
|
|
|
|
|
namespace {
|
2009-11-28 14:07:30 +08:00
|
|
|
class WalkAST : public StmtVisitor<WalkAST> {
|
2009-11-08 21:10:34 +08:00
|
|
|
BugReporter &BR;
|
2011-10-24 09:32:45 +08:00
|
|
|
AnalysisDeclContext* AC;
|
2009-11-08 21:10:34 +08:00
|
|
|
|
|
|
|
public:
|
2011-10-24 09:32:45 +08:00
|
|
|
WalkAST(BugReporter &br, AnalysisDeclContext* ac) : BR(br), AC(ac) {}
|
2011-03-12 03:24:49 +08:00
|
|
|
void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
|
2009-11-08 21:10:34 +08:00
|
|
|
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
|
2011-03-12 03:24:49 +08:00
|
|
|
void WalkAST::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
|
|
|
|
if (E->getKind() != UETT_SizeOf)
|
2009-11-08 21:10:34 +08:00
|
|
|
return;
|
|
|
|
|
2009-11-10 12:20:20 +08:00
|
|
|
// If an explicit type is used in the code, usually the coder knows what he is
|
|
|
|
// doing.
|
|
|
|
if (E->isArgumentType())
|
|
|
|
return;
|
|
|
|
|
2009-11-08 21:10:34 +08:00
|
|
|
QualType T = E->getTypeOfArgument();
|
|
|
|
if (T->isPointerType()) {
|
2009-11-10 15:52:53 +08:00
|
|
|
|
|
|
|
// 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();
|
2009-11-10 16:33:44 +08:00
|
|
|
if (!isa<DeclRefExpr>(ArgEx->IgnoreParens()))
|
2009-11-10 15:52:53 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
SourceRange R = ArgEx->getSourceRange();
|
2011-09-21 05:38:35 +08:00
|
|
|
PathDiagnosticLocation ELoc =
|
|
|
|
PathDiagnosticLocation::createBegin(E, BR.getSourceManager(), AC);
|
2012-04-05 02:11:35 +08:00
|
|
|
BR.EmitBasicReport(AC->getDecl(),
|
|
|
|
"Potential unintended use of sizeof() on pointer type",
|
2013-10-04 08:25:24 +08:00
|
|
|
categories::LogicError,
|
2009-11-09 15:29:39 +08:00
|
|
|
"The code calls sizeof() on a pointer type. "
|
|
|
|
"This can produce an unexpected result.",
|
2011-09-21 05:38:35 +08:00
|
|
|
ELoc, &R, 1);
|
2009-11-08 21:10:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-18 05:39:33 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// SizeofPointerChecker
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
2011-03-01 09:16:21 +08:00
|
|
|
class SizeofPointerChecker : public Checker<check::ASTCodeBody> {
|
2011-02-18 05:39:33 +08:00
|
|
|
public:
|
|
|
|
void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
|
|
|
|
BugReporter &BR) const {
|
2011-10-24 09:32:45 +08:00
|
|
|
WalkAST walker(BR, mgr.getAnalysisDeclContext(D));
|
2011-02-18 05:39:33 +08:00
|
|
|
walker.Visit(D->getBody());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void ento::registerSizeofPointerChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<SizeofPointerChecker>();
|
2009-11-08 21:10:34 +08:00
|
|
|
}
|