r208110 - Fix leak in ObjCSelfInitChecker, found by LSan.

Nico Weber nicolasweber at gmx.de
Tue May 6 10:33:42 PDT 2014


Author: nico
Date: Tue May  6 12:33:42 2014
New Revision: 208110

URL: http://llvm.org/viewvc/llvm-project?rev=208110&view=rev
Log:
Fix leak in ObjCSelfInitChecker, found by LSan.

BugReport doesn't take ownership of the bug type, so let the checker own the
the bug type.  (Requires making the bug type mutable, which is icky, but which
is also what other checkers do.)

Modified:
    cfe/trunk/lib/StaticAnalyzer/Checkers/ObjCSelfInitChecker.cpp

Modified: cfe/trunk/lib/StaticAnalyzer/Checkers/ObjCSelfInitChecker.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Checkers/ObjCSelfInitChecker.cpp?rev=208110&r1=208109&r2=208110&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Checkers/ObjCSelfInitChecker.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Checkers/ObjCSelfInitChecker.cpp Tue May  6 12:33:42 2014
@@ -55,6 +55,13 @@ static bool isInitMessage(const ObjCMeth
 static bool isSelfVar(SVal location, CheckerContext &C);
 
 namespace {
+class InitSelfBug : public BugType {
+public:
+  InitSelfBug(const CheckerBase *Checker)
+      : BugType(Checker, "Missing \"self = [(super or self) init...]\"",
+                categories::CoreFoundationObjectiveC) {}
+};
+
 class ObjCSelfInitChecker : public Checker<  check::PostObjCMessage,
                                              check::PostStmt<ObjCIvarRefExpr>,
                                              check::PreStmt<ReturnStmt>,
@@ -62,7 +69,13 @@ class ObjCSelfInitChecker : public Check
                                              check::PostCall,
                                              check::Location,
                                              check::Bind > {
+  mutable InitSelfBug InitSelfBugType;
+
+  void checkForInvalidSelf(const Expr *E, CheckerContext &C,
+                           const char *errorStr) const;
+
 public:
+  ObjCSelfInitChecker() : InitSelfBugType(this) {}
   void checkPostObjCMessage(const ObjCMethodCall &Msg, CheckerContext &C) const;
   void checkPostStmt(const ObjCIvarRefExpr *E, CheckerContext &C) const;
   void checkPreStmt(const ReturnStmt *S, CheckerContext &C) const;
@@ -79,17 +92,6 @@ public:
 } // end anonymous namespace
 
 namespace {
-
-class InitSelfBug : public BugType {
-public:
-  InitSelfBug(const CheckerBase *Checker)
-      : BugType(Checker, "Missing \"self = [(super or self) init...]\"",
-                categories::CoreFoundationObjectiveC) {}
-};
-
-} // end anonymous namespace
-
-namespace {
 enum SelfFlagEnum {
   /// \brief No flag set.
   SelfFlag_None = 0x0,
@@ -146,9 +148,8 @@ static bool isInvalidSelf(const Expr *E,
   return true;
 }
 
-static void checkForInvalidSelf(const Expr *E, CheckerContext &C,
-                                const char *errorStr,
-                                const CheckerBase *Checker) {
+void ObjCSelfInitChecker::checkForInvalidSelf(const Expr *E, CheckerContext &C,
+                                              const char *errorStr) const {
   if (!E)
     return;
   
@@ -163,7 +164,7 @@ static void checkForInvalidSelf(const Ex
   if (!N)
     return;
 
-  BugReport *report = new BugReport(*new InitSelfBug(Checker), errorStr, N);
+  BugReport *report = new BugReport(InitSelfBugType, errorStr, N);
   C.emitReport(report);
 }
 
@@ -208,8 +209,7 @@ void ObjCSelfInitChecker::checkPostStmt(
   checkForInvalidSelf(
       E->getBase(), C,
       "Instance variable used while 'self' is not set to the result of "
-      "'[(super or self) init...]'",
-      this);
+      "'[(super or self) init...]'");
 }
 
 void ObjCSelfInitChecker::checkPreStmt(const ReturnStmt *S,
@@ -221,8 +221,7 @@ void ObjCSelfInitChecker::checkPreStmt(c
 
   checkForInvalidSelf(S->getRetValue(), C,
                       "Returning 'self' while it is not set to the result of "
-                      "'[(super or self) init...]'",
-                      this);
+                      "'[(super or self) init...]'");
 }
 
 // When a call receives a reference to 'self', [Pre/Post]Call pass





More information about the cfe-commits mailing list