comerr: Don't leak CFStringRef
authorSimon Wilkinson <sxw@inf.ed.ac.uk>
Fri, 11 Jun 2010 22:59:49 +0000 (23:59 +0100)
committerDerrick Brashear <shadow@dementia.org>
Sun, 13 Jun 2010 05:35:51 +0000 (22:35 -0700)
If str is NULL on entry to _intlize, than it will leak the
CFStringRef that it constructed a few lines earlier. Fix this.

Caught with clang-analyzer

Change-Id: I6d62e7792918da95e4fed1c676d14ad9124d93fa
Reviewed-on: http://gerrit.openafs.org/2137
Reviewed-by: Russ Allbery <rra@stanford.edu>
Reviewed-by: Derrick Brashear <shadow@dementia.org>
Tested-by: Derrick Brashear <shadow@dementia.org>

src/comerr/error_msg.c

index 21d46fa..eae3204 100644 (file)
@@ -126,15 +126,25 @@ _intlize(const char *msg, int base, char *str, size_t len)
     CFStringRef cfdomain;
     CFBundleRef OpenAFSBundle = CFBundleGetBundleWithIdentifier(CFSTR("org.openafs.filesystems.afs"));
 
-    if (!str)
+    if (!str) {
+        CFRelease(cfstring);
        return msg;
+    }
+
     snprintf(domain, sizeof(domain), "heim_com_err%d", base);
     cfdomain = CFStringCreateWithCString(kCFAllocatorSystemDefault, domain,
                                         kCFStringEncodingUTF8);
-    if (OpenAFSBundle != NULL)
-       cfstring = CFBundleCopyLocalizedString(OpenAFSBundle, cfstring,
-                                              cfstring, cfdomain);
-    CFStringGetCString(cfstring, str, len, kCFStringEncodingUTF8);
+    if (OpenAFSBundle != NULL) {
+       CFStringRef cflocal;
+
+       cflocal = CFBundleCopyLocalizedString(OpenAFSBundle, cfstring,
+                                             cfstring, cfdomain);
+        CFStringGetCString(cflocal, str, len, kCFStringEncodingUTF8);
+       CFRelease(cflocal);
+    } else {
+        CFStringGetCString(cfstring, str, len, kCFStringEncodingUTF8);
+    }
+
     CFRelease(cfstring);
     CFRelease(cfdomain);
     return str;