blob: f2c50501a65c8aec4c5746e5f23b5ba31240862e [file] [log] [blame]
Zhongxing Xub0a05f7c2009-11-08 13:10:34 +00001//==- 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
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +000015#include "ClangSACheckers.h"
Anna Zaksc29bed32011-09-20 21:38:35 +000016#include "clang/AST/StmtVisitor.h"
Ted Kremenekf8cbac42011-02-10 01:03:03 +000017#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/StaticAnalyzer/Core/Checker.h"
Anna Zaksc29bed32011-09-20 21:38:35 +000019#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
Zhongxing Xub0a05f7c2009-11-08 13:10:34 +000020
21using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000022using namespace ento;
Zhongxing Xub0a05f7c2009-11-08 13:10:34 +000023
24namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000025class WalkAST : public StmtVisitor<WalkAST> {
Zhongxing Xub0a05f7c2009-11-08 13:10:34 +000026 BugReporter &BR;
Ted Kremenek81ce1c82011-10-24 01:32:45 +000027 AnalysisDeclContext* AC;
Zhongxing Xub0a05f7c2009-11-08 13:10:34 +000028
29public:
Ted Kremenek81ce1c82011-10-24 01:32:45 +000030 WalkAST(BugReporter &br, AnalysisDeclContext* ac) : BR(br), AC(ac) {}
Peter Collingbournee190dee2011-03-11 19:24:49 +000031 void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Zhongxing Xub0a05f7c2009-11-08 13:10:34 +000032 void VisitStmt(Stmt *S) { VisitChildren(S); }
33 void VisitChildren(Stmt *S);
34};
35}
36
37void WalkAST::VisitChildren(Stmt *S) {
38 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
39 if (Stmt *child = *I)
40 Visit(child);
41}
42
43// CWE-467: Use of sizeof() on a Pointer Type
Peter Collingbournee190dee2011-03-11 19:24:49 +000044void WalkAST::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
45 if (E->getKind() != UETT_SizeOf)
Zhongxing Xub0a05f7c2009-11-08 13:10:34 +000046 return;
47
Zhongxing Xu9a7448c2009-11-10 04:20:20 +000048 // If an explicit type is used in the code, usually the coder knows what he is
49 // doing.
50 if (E->isArgumentType())
51 return;
52
Zhongxing Xub0a05f7c2009-11-08 13:10:34 +000053 QualType T = E->getTypeOfArgument();
54 if (T->isPointerType()) {
Zhongxing Xu537db5d2009-11-10 07:52:53 +000055
56 // Many false positives have the form 'sizeof *p'. This is reasonable
57 // because people know what they are doing when they intentionally
58 // dereference the pointer.
59 Expr *ArgEx = E->getArgumentExpr();
Zhongxing Xuf9667222009-11-10 08:33:44 +000060 if (!isa<DeclRefExpr>(ArgEx->IgnoreParens()))
Zhongxing Xu537db5d2009-11-10 07:52:53 +000061 return;
62
63 SourceRange R = ArgEx->getSourceRange();
Anna Zaksc29bed32011-09-20 21:38:35 +000064 PathDiagnosticLocation ELoc =
65 PathDiagnosticLocation::createBegin(E, BR.getSourceManager(), AC);
Ted Kremenek5a10f082012-04-04 18:11:35 +000066 BR.EmitBasicReport(AC->getDecl(),
67 "Potential unintended use of sizeof() on pointer type",
Richard Smith25d8e732013-10-03 23:38:02 +000068 "Logic",
Zhongxing Xu85000202009-11-09 07:29:39 +000069 "The code calls sizeof() on a pointer type. "
70 "This can produce an unexpected result.",
Anna Zaksc29bed32011-09-20 21:38:35 +000071 ELoc, &R, 1);
Zhongxing Xub0a05f7c2009-11-08 13:10:34 +000072 }
73}
74
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +000075//===----------------------------------------------------------------------===//
76// SizeofPointerChecker
77//===----------------------------------------------------------------------===//
78
79namespace {
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000080class SizeofPointerChecker : public Checker<check::ASTCodeBody> {
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +000081public:
82 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
83 BugReporter &BR) const {
Ted Kremenek81ce1c82011-10-24 01:32:45 +000084 WalkAST walker(BR, mgr.getAnalysisDeclContext(D));
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +000085 walker.Visit(D->getBody());
86 }
87};
88}
89
90void ento::registerSizeofPointerChecker(CheckerManager &mgr) {
91 mgr.registerChecker<SizeofPointerChecker>();
Zhongxing Xub0a05f7c2009-11-08 13:10:34 +000092}