2009-11-08 21:10:34 +08:00
|
|
|
//==- CheckSizeofPointer.cpp - Check for sizeof on pointers ------*- C++ -*-==//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2009-11-08 21:10:34 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines a check for unintended use of sizeof() on pointer
|
|
|
|
// expressions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[analyzer][NFC] Move CheckerRegistry from the Core directory to Frontend
ClangCheckerRegistry is a very non-obvious, poorly documented, weird concept.
It derives from CheckerRegistry, and is placed in lib/StaticAnalyzer/Frontend,
whereas it's base is located in lib/StaticAnalyzer/Core. It was, from what I can
imagine, used to circumvent the problem that the registry functions of the
checkers are located in the clangStaticAnalyzerCheckers library, but that
library depends on clangStaticAnalyzerCore. However, clangStaticAnalyzerFrontend
depends on both of those libraries.
One can make the observation however, that CheckerRegistry has no place in Core,
it isn't used there at all! The only place where it is used is Frontend, which
is where it ultimately belongs.
This move implies that since
include/clang/StaticAnalyzer/Checkers/ClangCheckers.h only contained a single function:
class CheckerRegistry;
void registerBuiltinCheckers(CheckerRegistry ®istry);
it had to re purposed, as CheckerRegistry is no longer available to
clangStaticAnalyzerCheckers. It was renamed to BuiltinCheckerRegistration.h,
which actually describes it a lot better -- it does not contain the registration
functions for checkers, but only those generated by the tblgen files.
Differential Revision: https://reviews.llvm.org/D54436
llvm-svn: 349275
2018-12-16 00:23:51 +08:00
|
|
|
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.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;
|
2014-02-12 05:49:21 +08:00
|
|
|
const CheckerBase *Checker;
|
2011-10-24 09:32:45 +08:00
|
|
|
AnalysisDeclContext* AC;
|
2009-11-08 21:10:34 +08:00
|
|
|
|
|
|
|
public:
|
2014-02-12 05:49:21 +08:00
|
|
|
WalkAST(BugReporter &br, const CheckerBase *checker, AnalysisDeclContext *ac)
|
|
|
|
: BR(br), Checker(checker), 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);
|
|
|
|
};
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2009-11-08 21:10:34 +08:00
|
|
|
|
|
|
|
void WalkAST::VisitChildren(Stmt *S) {
|
2015-07-03 23:12:24 +08:00
|
|
|
for (Stmt *Child : S->children())
|
|
|
|
if (Child)
|
|
|
|
Visit(Child);
|
2009-11-08 21:10:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2014-05-21 01:10:39 +08:00
|
|
|
// If an explicit type is used in the code, usually the coder knows what they are
|
2009-11-10 12:20:20 +08:00
|
|
|
// 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
|
|
|
|
2015-09-08 11:50:52 +08:00
|
|
|
// Many false positives have the form 'sizeof *p'. This is reasonable
|
|
|
|
// because people know what they are doing when they intentionally
|
2009-11-10 15:52:53 +08:00
|
|
|
// 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;
|
|
|
|
|
2011-09-21 05:38:35 +08:00
|
|
|
PathDiagnosticLocation ELoc =
|
|
|
|
PathDiagnosticLocation::createBegin(E, BR.getSourceManager(), AC);
|
2014-02-12 05:49:21 +08:00
|
|
|
BR.EmitBasicReport(AC->getDecl(), Checker,
|
2012-04-05 02:11:35 +08:00
|
|
|
"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.",
|
2013-10-08 01:16:59 +08:00
|
|
|
ELoc, ArgEx->getSourceRange());
|
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 {
|
2014-02-12 05:49:21 +08:00
|
|
|
WalkAST walker(BR, this, 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
|
|
|
}
|
2019-01-26 22:23:08 +08:00
|
|
|
|
|
|
|
bool ento::shouldRegisterSizeofPointerChecker(const LangOptions &LO) {
|
|
|
|
return true;
|
|
|
|
}
|