1 //==- CheckSizeofPointer.cpp - Check for sizeof on pointers ------*- C++ -*-==//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines a check for unintended use of sizeof() on pointer
12 //===----------------------------------------------------------------------===//
14 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
15 #include "clang/AST/StmtVisitor.h"
16 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
17 #include "clang/StaticAnalyzer/Core/Checker.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
20 using namespace clang
;
24 class WalkAST
: public StmtVisitor
<WalkAST
> {
26 const CheckerBase
*Checker
;
27 AnalysisDeclContext
* AC
;
30 WalkAST(BugReporter
&br
, const CheckerBase
*checker
, AnalysisDeclContext
*ac
)
31 : BR(br
), Checker(checker
), AC(ac
) {}
32 void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr
*E
);
33 void VisitStmt(Stmt
*S
) { VisitChildren(S
); }
34 void VisitChildren(Stmt
*S
);
38 void WalkAST::VisitChildren(Stmt
*S
) {
39 for (Stmt
*Child
: S
->children())
44 // CWE-467: Use of sizeof() on a Pointer Type
45 void WalkAST::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr
*E
) {
46 if (E
->getKind() != UETT_SizeOf
)
49 // If an explicit type is used in the code, usually the coder knows what they are
51 if (E
->isArgumentType())
54 QualType T
= E
->getTypeOfArgument();
55 if (T
->isPointerType()) {
57 // Many false positives have the form 'sizeof *p'. This is reasonable
58 // because people know what they are doing when they intentionally
59 // dereference the pointer.
60 Expr
*ArgEx
= E
->getArgumentExpr();
61 if (!isa
<DeclRefExpr
>(ArgEx
->IgnoreParens()))
64 PathDiagnosticLocation ELoc
=
65 PathDiagnosticLocation::createBegin(E
, BR
.getSourceManager(), AC
);
66 BR
.EmitBasicReport(AC
->getDecl(), Checker
,
67 "Potential unintended use of sizeof() on pointer type",
68 categories::LogicError
,
69 "The code calls sizeof() on a pointer type. "
70 "This can produce an unexpected result.",
71 ELoc
, ArgEx
->getSourceRange());
75 //===----------------------------------------------------------------------===//
76 // SizeofPointerChecker
77 //===----------------------------------------------------------------------===//
80 class SizeofPointerChecker
: public Checker
<check::ASTCodeBody
> {
82 void checkASTCodeBody(const Decl
*D
, AnalysisManager
& mgr
,
83 BugReporter
&BR
) const {
84 WalkAST
walker(BR
, this, mgr
.getAnalysisDeclContext(D
));
85 walker
.Visit(D
->getBody());
90 void ento::registerSizeofPointerChecker(CheckerManager
&mgr
) {
91 mgr
.registerChecker
<SizeofPointerChecker
>();
94 bool ento::shouldRegisterSizeofPointerChecker(const CheckerManager
&mgr
) {