blob: e079a8cb12be37e7bc599894427cdd2493c34974 [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;
Alexander Kornienko4aca9b12014-02-11 21:49:21 +000027 const CheckerBase *Checker;
Ted Kremenek81ce1c82011-10-24 01:32:45 +000028 AnalysisDeclContext* AC;
Zhongxing Xub0a05f7c2009-11-08 13:10:34 +000029
30public:
Alexander Kornienko4aca9b12014-02-11 21:49:21 +000031 WalkAST(BugReporter &br, const CheckerBase *checker, AnalysisDeclContext *ac)
32 : BR(br), Checker(checker), AC(ac) {}
Peter Collingbournee190dee2011-03-11 19:24:49 +000033 void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Zhongxing Xub0a05f7c2009-11-08 13:10:34 +000034 void VisitStmt(Stmt *S) { VisitChildren(S); }
35 void VisitChildren(Stmt *S);
36};
Alexander Kornienkoab9db512015-06-22 23:07:51 +000037}
Zhongxing Xub0a05f7c2009-11-08 13:10:34 +000038
39void WalkAST::VisitChildren(Stmt *S) {
Benjamin Kramer973431b2015-07-03 15:12:24 +000040 for (Stmt *Child : S->children())
41 if (Child)
42 Visit(Child);
Zhongxing Xub0a05f7c2009-11-08 13:10:34 +000043}
44
45// CWE-467: Use of sizeof() on a Pointer Type
Peter Collingbournee190dee2011-03-11 19:24:49 +000046void WalkAST::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
47 if (E->getKind() != UETT_SizeOf)
Zhongxing Xub0a05f7c2009-11-08 13:10:34 +000048 return;
49
Eric Christopherc9e2a682014-05-20 17:10:39 +000050 // If an explicit type is used in the code, usually the coder knows what they are
Zhongxing Xu9a7448c2009-11-10 04:20:20 +000051 // doing.
52 if (E->isArgumentType())
53 return;
54
Zhongxing Xub0a05f7c2009-11-08 13:10:34 +000055 QualType T = E->getTypeOfArgument();
56 if (T->isPointerType()) {
Zhongxing Xu537db5d2009-11-10 07:52:53 +000057
Ted Kremenek3a0678e2015-09-08 03:50:52 +000058 // Many false positives have the form 'sizeof *p'. This is reasonable
59 // because people know what they are doing when they intentionally
Zhongxing Xu537db5d2009-11-10 07:52:53 +000060 // dereference the pointer.
61 Expr *ArgEx = E->getArgumentExpr();
Zhongxing Xuf9667222009-11-10 08:33:44 +000062 if (!isa<DeclRefExpr>(ArgEx->IgnoreParens()))
Zhongxing Xu537db5d2009-11-10 07:52:53 +000063 return;
64
Anna Zaksc29bed32011-09-20 21:38:35 +000065 PathDiagnosticLocation ELoc =
66 PathDiagnosticLocation::createBegin(E, BR.getSourceManager(), AC);
Alexander Kornienko4aca9b12014-02-11 21:49:21 +000067 BR.EmitBasicReport(AC->getDecl(), Checker,
Ted Kremenek5a10f082012-04-04 18:11:35 +000068 "Potential unintended use of sizeof() on pointer type",
Jordan Rose6feda282013-10-04 00:25:24 +000069 categories::LogicError,
Zhongxing Xu85000202009-11-09 07:29:39 +000070 "The code calls sizeof() on a pointer type. "
71 "This can produce an unexpected result.",
Jordan Rose42b42482013-10-07 17:16:59 +000072 ELoc, ArgEx->getSourceRange());
Zhongxing Xub0a05f7c2009-11-08 13:10:34 +000073 }
74}
75
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +000076//===----------------------------------------------------------------------===//
77// SizeofPointerChecker
78//===----------------------------------------------------------------------===//
79
80namespace {
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000081class SizeofPointerChecker : public Checker<check::ASTCodeBody> {
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +000082public:
83 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
84 BugReporter &BR) const {
Alexander Kornienko4aca9b12014-02-11 21:49:21 +000085 WalkAST walker(BR, this, mgr.getAnalysisDeclContext(D));
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +000086 walker.Visit(D->getBody());
87 }
88};
89}
90
91void ento::registerSizeofPointerChecker(CheckerManager &mgr) {
92 mgr.registerChecker<SizeofPointerChecker>();
Zhongxing Xub0a05f7c2009-11-08 13:10:34 +000093}