From 209b5ccd827039b933b730e3f29abd0d72d3c6c6 Mon Sep 17 00:00:00 2001 From: Vitali Lovich Date: Tue, 14 Jun 2011 08:51:57 +0200 Subject: [PATCH] Darwin: Fix #108 crash when a device is unplugged --- libusb/os/darwin_usb.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/libusb/os/darwin_usb.c b/libusb/os/darwin_usb.c index e7318d8..480ec52 100644 --- a/libusb/os/darwin_usb.c +++ b/libusb/os/darwin_usb.c @@ -227,6 +227,7 @@ static void darwin_devices_detached (void *ptr, io_iterator_t rem_devices) { io_service_t device; long location; + bool locationValid; CFTypeRef locationCF; UInt32 message; @@ -236,10 +237,19 @@ static void darwin_devices_detached (void *ptr, io_iterator_t rem_devices) { /* get the location from the i/o registry */ locationCF = IORegistryEntryCreateCFProperty (device, CFSTR(kUSBDevicePropertyLocationID), kCFAllocatorDefault, 0); - CFNumberGetValue(locationCF, kCFNumberLongType, &location); - CFRelease (locationCF); IOObjectRelease (device); + if (!locationCF) + continue; + + locationValid = CFGetTypeID(locationCF) == CFNumberGetTypeID() && + CFNumberGetValue(locationCF, kCFNumberLongType, &location); + + CFRelease (locationCF); + + if (!locationValid) + continue; + usbi_mutex_lock(&ctx->open_devs_lock); list_for_each_entry(handle, &ctx->open_devs, list, struct libusb_device_handle) { dpriv = (struct darwin_device_priv *)handle->dev->os_priv; -- 2.7.4