]> granicus.if.org Git - clang/commitdiff
[analyzer] RetainCountChecker: recognize that OSObject can be created directly using...
authorGeorge Karpenkov <ekarpenkov@apple.com>
Fri, 30 Nov 2018 02:19:03 +0000 (02:19 +0000)
committerGeorge Karpenkov <ekarpenkov@apple.com>
Fri, 30 Nov 2018 02:19:03 +0000 (02:19 +0000)
Differential Revision: https://reviews.llvm.org/D55076

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@347949 91177308-0d34-0410-b5e6-96231b3b80d8

lib/StaticAnalyzer/Checkers/RetainCountChecker/RetainCountDiagnostics.cpp
lib/StaticAnalyzer/Core/RetainSummaryManager.cpp
test/Analysis/osobject-retain-release.cpp

index da47513e30379740887181d5627817640687193e..cffbf58dff7d334fe9c56e75f71f855c3a867fdf 100644 (file)
@@ -137,6 +137,8 @@ static void generateDiagnosticsForCallLike(
     } else {
       os << "function call";
     }
+  } else if (const auto *NE = dyn_cast<CXXNewExpr>(S)){
+    os << "Operator new";
   } else {
     assert(isa<ObjCMessageExpr>(S));
     CallEventManager &Mgr = CurrSt->getStateManager().getCallEventManager();
index fc4544f843dba7519a23111375a16b4fadd4255b..357bc67559a896fa0cf327a57749d4c240f1b369 100644 (file)
@@ -124,10 +124,8 @@ RetainSummaryManager::generateSummary(const FunctionDecl *FD,
   }
 
   const IdentifierInfo *II = FD->getIdentifier();
-  if (!II)
-    return getDefaultSummary();
 
-  StringRef FName = II->getName();
+  StringRef FName = II ? II->getName() : "";
 
   // Strip away preceding '_'.  Doing this here will effect all the checks
   // down below.
@@ -304,6 +302,9 @@ RetainSummaryManager::generateSummary(const FunctionDecl *FD,
 
       if (FName == "retain")
         return getOSSummaryRetainRule(FD);
+
+      if (MD->getOverloadedOperator() == OO_New)
+        return getOSSummaryCreateRule(MD);
     }
   }
 
@@ -491,9 +492,11 @@ RetainSummaryManager::getSummary(const CallEvent &Call,
   case CE_CXXConstructor:
     Summ = getFunctionSummary(cast<CXXConstructorCall>(Call).getDecl());
     break;
+  case CE_CXXAllocator:
+    Summ = getFunctionSummary(cast<CXXAllocatorCall>(Call).getDecl());
+    break;
   case CE_Block:
   case CE_CXXDestructor:
-  case CE_CXXAllocator:
     // FIXME: These calls are currently unsupported.
     return getPersistentStopSummary();
   case CE_ObjCMessage: {
index ca5dfbf707c5e6ceb88c9f9796f9b2119ca4959c..81cf2a1f210a0dad196abe2609cd318c0f4f06ff 100644 (file)
@@ -23,6 +23,9 @@ struct OSObject {
   static OSObject *getObject();
   static OSObject *GetObject();
 
+
+  static void * operator new(unsigned long size);
+
   static const OSMetaClass * const metaClass;
 };
 
@@ -62,6 +65,18 @@ struct OSMetaClassBase {
   static OSObject *safeMetaCast(const OSObject *inst, const OSMetaClass *meta);
 };
 
+unsigned int check_leak_explicit_new() {
+  OSArray *arr = new OSArray; // expected-note{{Operator new returns an OSObject of type struct OSArray * with a +1 retain count}}
+  return arr->getCount(); // expected-note{{Object leaked: allocated object of type struct OSArray * is not referenced later in this execution path and has a retain count of +1}}
+                          // expected-warning@-1{{Potential leak of an object of type struct OSArray *}}
+}
+
+unsigned int check_leak_factory() {
+  OSArray *arr = OSArray::withCapacity(10); // expected-note{{Call to function 'OSArray::withCapacity' returns an OSObject of type struct OSArray * with a +1 retain count}}
+  return arr->getCount(); // expected-note{{Object leaked: object allocated and stored into 'arr' is not referenced later in this execution path and has a retain count of +1}}
+                          // expected-warning@-1{{Potential leak of an object stored into 'arr'}}
+}
+
 void check_get_object() {
   OSObject::getObject();
 }