blob: abf53fd3db28f509c02113683c8be5867d6d6935 [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"
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000016#include "clang/StaticAnalyzer/Core/Checker.h"
Ted Kremenekf8cbac42011-02-10 01:03:03 +000017#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
Zhongxing Xub0a05f7c2009-11-08 13:10:34 +000018#include "clang/AST/StmtVisitor.h"
Zhongxing Xub0a05f7c2009-11-08 13:10:34 +000019
20using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000021using namespace ento;
Zhongxing Xub0a05f7c2009-11-08 13:10:34 +000022
23namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000024class WalkAST : public StmtVisitor<WalkAST> {
Zhongxing Xub0a05f7c2009-11-08 13:10:34 +000025 BugReporter &BR;
26
27public:
28 WalkAST(BugReporter &br) : BR(br) {}
Peter Collingbournee190dee2011-03-11 19:24:49 +000029 void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E);
Zhongxing Xub0a05f7c2009-11-08 13:10:34 +000030 void VisitStmt(Stmt *S) { VisitChildren(S); }
31 void VisitChildren(Stmt *S);
32};
33}
34
35void WalkAST::VisitChildren(Stmt *S) {
36 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
37 if (Stmt *child = *I)
38 Visit(child);
39}
40
41// CWE-467: Use of sizeof() on a Pointer Type
Peter Collingbournee190dee2011-03-11 19:24:49 +000042void WalkAST::VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *E) {
43 if (E->getKind() != UETT_SizeOf)
Zhongxing Xub0a05f7c2009-11-08 13:10:34 +000044 return;
45
Zhongxing Xu9a7448c2009-11-10 04:20:20 +000046 // If an explicit type is used in the code, usually the coder knows what he is
47 // doing.
48 if (E->isArgumentType())
49 return;
50
Zhongxing Xub0a05f7c2009-11-08 13:10:34 +000051 QualType T = E->getTypeOfArgument();
52 if (T->isPointerType()) {
Zhongxing Xu537db5d2009-11-10 07:52:53 +000053
54 // Many false positives have the form 'sizeof *p'. This is reasonable
55 // because people know what they are doing when they intentionally
56 // dereference the pointer.
57 Expr *ArgEx = E->getArgumentExpr();
Zhongxing Xuf9667222009-11-10 08:33:44 +000058 if (!isa<DeclRefExpr>(ArgEx->IgnoreParens()))
Zhongxing Xu537db5d2009-11-10 07:52:53 +000059 return;
60
61 SourceRange R = ArgEx->getSourceRange();
Zhongxing Xub0a05f7c2009-11-08 13:10:34 +000062 BR.EmitBasicReport("Potential unintended use of sizeof() on pointer type",
63 "Logic",
Zhongxing Xu85000202009-11-09 07:29:39 +000064 "The code calls sizeof() on a pointer type. "
65 "This can produce an unexpected result.",
Zhongxing Xub0a05f7c2009-11-08 13:10:34 +000066 E->getLocStart(), &R, 1);
67 }
68}
69
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +000070//===----------------------------------------------------------------------===//
71// SizeofPointerChecker
72//===----------------------------------------------------------------------===//
73
74namespace {
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000075class SizeofPointerChecker : public Checker<check::ASTCodeBody> {
Argyrios Kyrtzidisaf45aca2011-02-17 21:39:33 +000076public:
77 void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
78 BugReporter &BR) const {
79 WalkAST walker(BR);
80 walker.Visit(D->getBody());
81 }
82};
83}
84
85void ento::registerSizeofPointerChecker(CheckerManager &mgr) {
86 mgr.registerChecker<SizeofPointerChecker>();
Zhongxing Xub0a05f7c2009-11-08 13:10:34 +000087}