]> git.ipfire.org Git - thirdparty/cups.git/commitdiff
Free memory leaked by Darwin's Core Foundation 655/head
authorRose <83477269+AtariDreams@users.noreply.github.com>
Wed, 19 Apr 2023 17:07:38 +0000 (13:07 -0400)
committerRose <83477269+AtariDreams@users.noreply.github.com>
Fri, 12 May 2023 16:29:43 +0000 (12:29 -0400)
There are cases where we do not always free the CF objects we allocate, causing them to leak.

backend/usb-darwin.c
cups/tls-darwin.c
ppdc/ppdc-catalog.cxx

index ec3d89b14a8dc1755202d38127f0500b07dc0e64..eb2abd40c9bb521b3ee1de9ca4eda3b595a5572c 100644 (file)
@@ -1438,23 +1438,23 @@ static kern_return_t load_classdriver(CFStringRef           driverPath,
   * Try loading the class driver...
   */
 
-  url = CFURLCreateWithFileSystemPath(NULL, bundle, kCFURLPOSIXPathStyle, true);
+  url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, bundle, kCFURLPOSIXPathStyle, true);
 
-  if (url)
-  {
-    plugin = CFPlugInCreate(NULL, url);
-    CFRelease(url);
-  }
-  else
-    plugin = NULL;
+  if (url == NULL)
+    return (kr);
+
+  plugin = CFPlugInCreate(kCFAllocatorDefault, url);
+  CFRelease(url);
 
   if (plugin)
   {
     CFArrayRef factories = CFPlugInFindFactoriesForPlugInTypeInPlugIn(kUSBPrinterClassTypeID, plugin);
-    if (factories != NULL && CFArrayGetCount(factories) > 0)
+    if (factories == NULL)
+      return (kr);
+    if (CFArrayGetCount(factories) > 0)
     {
       CFUUIDRef factoryID = CFArrayGetValueAtIndex(factories, 0);
-      IUnknownVTbl **iunknown = CFPlugInInstanceCreate(NULL, factoryID, kUSBPrinterClassTypeID);
+      IUnknownVTbl **iunknown = CFPlugInInstanceCreate(kCFAllocatorDefault, factoryID, kUSBPrinterClassTypeID);
       if (iunknown != NULL)
       {
        kr = (*iunknown)->QueryInterface(iunknown, CFUUIDGetUUIDBytes(kUSBPrinterClassInterfaceID), (LPVOID *)&driver);
@@ -1476,8 +1476,8 @@ static kern_return_t load_classdriver(CFStringRef     driverPath,
        }
        (*iunknown)->Release(iunknown);
       }
-      CFRelease(factories);
     }
+    CFRelease(factories);
   }
 
   fprintf(stderr, "DEBUG: load_classdriver(%s) (kr:0x%08x)\n", bundlestr, (int)kr);
@@ -1883,21 +1883,26 @@ static kern_return_t registry_close(void)
  */
 
 static CFStringRef copy_value_for_key(CFStringRef deviceID,
-                                     CFStringRef *keys)
+                                      CFStringRef *keys)
 {
-  CFStringRef  value = NULL;
-  CFArrayRef   kvPairs = deviceID != NULL ? CFStringCreateArrayBySeparatingStrings(NULL, deviceID, CFSTR(";")) : NULL;
-  CFIndex      max = kvPairs != NULL ? CFArrayGetCount(kvPairs) : 0;
-  CFIndex      idx = 0;
+  CFStringRef value = NULL; /* Value to return */
+  CFArrayRef kvPairs;       /* pairs derived from separating the device ID*/
+  CFIndex max;              /* The size of the array*/
 
-  while (idx < max && value == NULL)
+  if (deviceID == NULL)
+    return NULL;
+
+  kvPairs = CFStringCreateArrayBySeparatingStrings(kCFAllocatorDefault, deviceID, CFSTR(";"));
+  max = CFArrayGetCount(kvPairs);
+
+  for (CFIndex idx = 0; idx < max; idx++)
   {
     CFStringRef kvpair = CFArrayGetValueAtIndex(kvPairs, idx);
     CFIndex idxx = 0;
-    while (keys[idxx] != NULL && value == NULL)
+    for (idxx = 0; keys[idxx] != NULL; idxx++)
     {
       CFRange range = CFStringFind(kvpair, keys[idxx], kCFCompareCaseInsensitive);
-      if (range.length != -1)
+      if (range.length != kCFNotFound)
       {
        if (range.location != 0)
        {
@@ -1919,14 +1924,17 @@ static CFStringRef copy_value_for_key(CFStringRef deviceID,
          value = theString2;
        }
       }
-      idxx++;
+
+      if (value != NULL)
+      {
+        CFRelease(kvPairs);
+        return value;
+      }
     }
-    idx++;
   }
 
-  if (kvPairs != NULL)
-    CFRelease(kvPairs);
-  return value;
+  CFRelease(kvPairs);
+  return NULL;
 }
 
 
@@ -2046,26 +2054,28 @@ static void parse_options(char *options,
  */
 static void setup_cfLanguage(void)
 {
-  CFStringRef  lang[1] = {NULL};
-  CFArrayRef   langArray = NULL;
-  const char   *requestedLang = NULL;
+  CFStringRef lang[1] = {NULL}; /* StringRef used to create the array */
+  CFArrayRef langArray;      /* The array used to set the language perference */
+  const char *requestedLang; /* The language as retrived from the language
+                                environment variable */
 
   if ((requestedLang = getenv("APPLE_LANGUAGE")) == NULL)
     requestedLang = getenv("LANG");
 
-  if (requestedLang != NULL)
+  if (requestedLang == NULL)
   {
-    lang[0] = CFStringCreateWithCString(kCFAllocatorDefault, requestedLang, kCFStringEncodingUTF8);
-    langArray = CFArrayCreate(kCFAllocatorDefault, (const void **)lang, sizeof(lang) / sizeof(lang[0]), &kCFTypeArrayCallBacks);
+    fputs("DEBUG: usb: LANG and APPLE_LANGUAGE environment variables missing.\n", stderr);
+    return;
+  }
 
-    CFPreferencesSetValue(CFSTR("AppleLanguages"), langArray, kCFPreferencesCurrentApplication, kCFPreferencesAnyUser, kCFPreferencesAnyHost);
-    fprintf(stderr, "DEBUG: usb: AppleLanguages=\"%s\"\n", requestedLang);
+  lang[0] = CFStringCreateWithCString(kCFAllocatorDefault, requestedLang, kCFStringEncodingUTF8);
+  langArray = CFArrayCreate(kCFAllocatorDefault, (const void **)lang, sizeof(lang) / sizeof(lang[0]), &kCFTypeArrayCallBacks);
 
-    CFRelease(lang[0]);
-    CFRelease(langArray);
-  }
-  else
-    fputs("DEBUG: usb: LANG and APPLE_LANGUAGE environment variables missing.\n", stderr);
+  CFPreferencesSetValue(CFSTR("AppleLanguages"), langArray, kCFPreferencesCurrentApplication, kCFPreferencesAnyUser, kCFPreferencesAnyHost);
+  fprintf(stderr, "DEBUG: usb: AppleLanguages=\"%s\"\n", requestedLang);
+
+  CFRelease(lang[0]);
+  CFRelease(langArray);
 }
 
 #pragma mark -
@@ -2083,7 +2093,7 @@ static void run_legacy_backend(int argc,
                               char *argv[],
                               int fd)
 {
-  int  i;
+  size_t i;
   int  exitstatus = 0;
   int  childstatus;
   pid_t        waitpid_status;
@@ -2158,7 +2168,7 @@ static void run_legacy_backend(int argc,
       cups_serverbin = CUPS_SERVERBIN;
     snprintf(usbpath, sizeof(usbpath), "%s/backend/usb", cups_serverbin);
 
-    for (i = 0; i < argc && i < (int)(sizeof(my_argv) / sizeof(my_argv[0])) - 1; i ++)
+    for (i = 0; i < argc && i < (sizeof(my_argv) / sizeof(my_argv[0])) - 1; i++)
       my_argv[i] = argv[i];
 
     my_argv[i] = NULL;
index 805796a2861b36f9f8a50b49bf682e5cfda0c7f4..fb5caac07162702f287b7c63eb85959f0015a5ce 100644 (file)
@@ -899,10 +899,8 @@ void
 _httpFreeCredentials(
     http_tls_credentials_t credentials)        /* I - Internal credentials */
 {
-  if (!credentials)
-    return;
-
-  CFRelease(credentials);
+  if (credentials)
+    CFRelease(credentials);
 }
 
 
@@ -1671,9 +1669,10 @@ _httpTLSStart(http_t *http)              /* I - HTTP connection */
            if (cg->client_cert_cb)
            {
              names = NULL;
-             if (!(error = SSLCopyDistinguishedNames(http->tls, &dn_array)) &&
-                 dn_array)
-             {
+        error = SSLCopyDistinguishedNames(http->tls, &dn_array);
+        if (dn_array)
+        {
+          if (!error) {
                if ((names = cupsArrayNew(NULL, NULL)) != NULL)
                {
                  for (i = 0, count = CFArrayGetCount(dn_array); i < count; i++)
@@ -1694,9 +1693,9 @@ _httpTLSStart(http_t *http)               /* I - HTTP connection */
                    }
                  }
                }
-
-               CFRelease(dn_array);
              }
+               CFRelease(dn_array);
+        }
 
              if (!error)
              {
@@ -1792,12 +1791,13 @@ _httpTLSStop(http_t *http)              /* I - HTTP connection */
     usleep(1000);
 
   CFRelease(http->tls);
+  http->tls = NULL;
 
   if (http->tls_credentials)
+  {
     CFRelease(http->tls_credentials);
-
-  http->tls             = NULL;
-  http->tls_credentials = NULL;
+    http->tls_credentials = NULL;
+  }
 }
 
 
index 4aac366260a08d7ad71f6bcb18ed1a8955177a51..1788d0d37d02390838ae727378c8e7e1f261ac99 100644 (file)
@@ -107,13 +107,14 @@ ppdcCatalog::ppdcCatalog(const char *l,   // I - Locale
 
        plist = CFPropertyListCreateWithStream(kCFAllocatorDefault, stream, 0, kCFPropertyListImmutable, NULL, NULL);
 
-       if (plist && CFGetTypeID(plist) == CFDictionaryGetTypeID())
-         CFDictionaryApplyFunction((CFDictionaryRef)plist, (CFDictionaryApplierFunction)apple_add_message, this);
-
-       if (plist)
-         CFRelease(plist);
+  if (plist)
+  {
+    if (CFGetTypeID(plist) == CFDictionaryGetTypeID())
+      CFDictionaryApplyFunction((CFDictionaryRef)plist, (CFDictionaryApplierFunction)apple_add_message, this);
+    CFRelease(plist);
+  }
 
-       CFRelease(stream);
+  CFRelease(stream);
       }
 
       CFRelease(url);