darwin: Improve error checking to prevent (very unlikely) null dereference
authorSean McBride <sean@rogue-research.com>
Fri, 5 Jan 2018 03:17:24 +0000 (22:17 -0500)
committerChris Dickens <christopher.a.dickens@gmail.com>
Tue, 9 Jan 2018 06:11:40 +0000 (22:11 -0800)
Defend against the very unlikely possibility of CFNumberCreate() returning null.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
libusb/os/darwin_usb.c
libusb/version_nano.h

index 6279a4f..35ea1c3 100644 (file)
@@ -233,20 +233,21 @@ static int usb_setup_device_iterator (io_iterator_t *deviceIterator, UInt32 loca
                                                                          &kCFTypeDictionaryKeyCallBacks,
                                                                          &kCFTypeDictionaryValueCallBacks);
 
-    if (propertyMatchDict) {
-      /* there are no unsigned CFNumber types so treat the value as signed. the os seems to do this
-         internally (CFNumberType of locationID is 3) */
-      CFTypeRef locationCF = CFNumberCreate (NULL, kCFNumberSInt32Type, &location);
+    /* there are no unsigned CFNumber types so treat the value as signed. the OS seems to do this
+         internally (CFNumberType of locationID is kCFNumberSInt32Type) */
+    CFTypeRef locationCF = CFNumberCreate (NULL, kCFNumberSInt32Type, &location);
 
+    if (propertyMatchDict && locationCF) {
       CFDictionarySetValue (propertyMatchDict, CFSTR(kUSBDevicePropertyLocationID), locationCF);
-      /* release our reference to the CFNumber (CFDictionarySetValue retains it) */
-      CFRelease (locationCF);
-
       CFDictionarySetValue (matchingDict, CFSTR(kIOPropertyMatchKey), propertyMatchDict);
-      /* release out reference to the CFMutableDictionaryRef (CFDictionarySetValue retains it) */
-      CFRelease (propertyMatchDict);
     }
     /* else we can still proceed as long as the caller accounts for the possibility of other devices in the iterator */
+
+    /* release our references as per the Create Rule */
+    if (propertyMatchDict)
+      CFRelease (propertyMatchDict);
+    if (locationCF)
+      CFRelease (locationCF);
   }
 
   return IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, deviceIterator);
index f263a06..7e638ba 100644 (file)
@@ -1 +1 @@
-#define LIBUSB_NANO 11292
+#define LIBUSB_NANO 11293