From e0d118397b4d314f40ffb0c8ed98dd351ed9105a Mon Sep 17 00:00:00 2001 From: Rose <83477269+AtariDreams@users.noreply.github.com> Date: Wed, 19 Apr 2023 13:07:38 -0400 Subject: [PATCH] Free memory leaked by Darwin's Core Foundation There are cases where we do not always free the CF objects we allocate, causing them to leak. --- backend/usb-darwin.c | 88 ++++++++++++++++++++++++------------------- cups/tls-darwin.c | 24 ++++++------ ppdc/ppdc-catalog.cxx | 13 ++++--- 3 files changed, 68 insertions(+), 57 deletions(-) diff --git a/backend/usb-darwin.c b/backend/usb-darwin.c index ec3d89b14a..eb2abd40c9 100644 --- a/backend/usb-darwin.c +++ b/backend/usb-darwin.c @@ -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; diff --git a/cups/tls-darwin.c b/cups/tls-darwin.c index 805796a286..fb5caac071 100644 --- a/cups/tls-darwin.c +++ b/cups/tls-darwin.c @@ -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; + } } diff --git a/ppdc/ppdc-catalog.cxx b/ppdc/ppdc-catalog.cxx index 4aac366260..1788d0d37d 100644 --- a/ppdc/ppdc-catalog.cxx +++ b/ppdc/ppdc-catalog.cxx @@ -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); -- 2.47.2