]> granicus.if.org Git - clang/commitdiff
Add a checker for CWE-467: Use of sizeof() on a Pointer Type.
authorZhongxing Xu <xuzhongxing@gmail.com>
Sun, 8 Nov 2009 13:10:34 +0000 (13:10 +0000)
committerZhongxing Xu <xuzhongxing@gmail.com>
Sun, 8 Nov 2009 13:10:34 +0000 (13:10 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@86464 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Analysis/LocalCheckers.h
include/clang/Frontend/Analyses.def
lib/Analysis/CheckSizeofPointer.cpp [new file with mode: 0644]
lib/Frontend/AnalysisConsumer.cpp

index 1da15fbae6c4c0a0238e90db731f151aabb68830..cd2d6b3ec70d7433bb933887546ad07ead4820ee 100644 (file)
@@ -53,7 +53,7 @@ void RegisterAppleChecks(GRExprEngine& Eng, const Decl &D);
 
 void CheckSecuritySyntaxOnly(const Decl *D, BugReporter &BR);
 
-
+void CheckSizeofPointer(const Decl *D, BugReporter &BR);
 } // end namespace clang
 
 #endif
index d5e408020a987053f41e9f979639c9461f491270..7d55673a612fda5649c5bb6a7ee87f1a4aed7bd9 100644 (file)
@@ -48,6 +48,9 @@ ANALYSIS(WarnObjCUnusedIvars, "warn-objc-unused-ivars",
 ANALYSIS(CheckerCFRef, "checker-cfref",
          "Run the [Core] Foundation reference count checker", Code)
 
+ANALYSIS(WarnSizeofPointer, "warn-sizeof-pointer",
+         "Warn about unintended use of sizeof() on pointer expressions", Code)
+
 ANALYSIS(InlineCall, "inline-call",
          "Experimental transfer function inling callees when its definition"
          " is available.", TranslationUnit)
diff --git a/lib/Analysis/CheckSizeofPointer.cpp b/lib/Analysis/CheckSizeofPointer.cpp
new file mode 100644 (file)
index 0000000..c61f6f5
--- /dev/null
@@ -0,0 +1,58 @@
+//==- CheckSizeofPointer.cpp - Check for sizeof on pointers ------*- C++ -*-==//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  This file defines a check for unintended use of sizeof() on pointer
+//  expressions.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Analysis/PathSensitive/BugReporter.h"
+#include "clang/AST/StmtVisitor.h"
+#include "clang/Analysis/LocalCheckers.h"
+#include "llvm/Support/Compiler.h"
+
+using namespace clang;
+
+namespace {
+class VISIBILITY_HIDDEN WalkAST : public StmtVisitor<WalkAST> {
+  BugReporter &BR;
+
+public:
+  WalkAST(BugReporter &br) : BR(br) {}
+  void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E);
+  void VisitStmt(Stmt *S) { VisitChildren(S); }
+  void VisitChildren(Stmt *S);
+};
+}
+
+void WalkAST::VisitChildren(Stmt *S) {
+  for (Stmt::child_iterator I = S->child_begin(), E = S->child_end(); I!=E; ++I)
+    if (Stmt *child = *I)
+      Visit(child);
+}
+
+// CWE-467: Use of sizeof() on a Pointer Type
+void WalkAST::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) {
+  if (!E->isSizeOf())
+    return;
+
+  QualType T = E->getTypeOfArgument();
+  if (T->isPointerType()) {
+    SourceRange R = E->getArgumentExpr()->getSourceRange();
+    BR.EmitBasicReport("Potential unintended use of sizeof() on pointer type",
+                       "Logic",
+                       "The code calls sizeof() on a malloced pointer type, which always returns the wordsize/8. This can produce an unexpected result if the programmer intended to determine how much memory has been allocated.",
+                       E->getLocStart(), &R, 1);
+  }
+}
+
+void clang::CheckSizeofPointer(const Decl *D, BugReporter &BR) {
+  WalkAST walker(BR);
+  walker.Visit(D->getBody());
+}
index d2831fae566ac022464e2f43f97d1b2e1b00e299..abe04b02ab4a9e4e8fb342b47428758b4f885e96 100644 (file)
@@ -412,6 +412,11 @@ static void ActionWarnObjCMethSigs(AnalysisManager& mgr, Decl *D) {
   CheckObjCInstMethSignature(cast<ObjCImplementationDecl>(D), BR);
 }
 
+static void ActionWarnSizeofPointer(AnalysisManager &mgr, Decl *D) {
+  BugReporter BR(mgr);
+  CheckSizeofPointer(D, BR);
+}
+
 static void ActionInlineCall(AnalysisManager &mgr, Decl *D) {
   if (!D)
     return;