From e3dda19b31ee974fd749d55bc97d8bfc0e976bb4 Mon Sep 17 00:00:00 2001 From: Nathan Hjelm Date: Fri, 11 Jun 2021 22:27:19 -0600 Subject: [PATCH] darwin: release device parent reference when re-enumerating device This commit fixes a dangling device reference that can occur when a device is being re-enumerated. The problem is the code was unconditionally re-caching the parent device which then takes a reference on the parent device. If the field was already populated this reference was not needed. To ensure the code works even if the parent device also changes during the re-enumeration the code release the parent reference and grabs a fresh reference. Fixes #924 Signed-off-by: Nathan Hjelm --- libusb/os/darwin_usb.c | 7 +++++-- libusb/version_nano.h | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/libusb/os/darwin_usb.c b/libusb/os/darwin_usb.c index 7202337..6de8102 100644 --- a/libusb/os/darwin_usb.c +++ b/libusb/os/darwin_usb.c @@ -1131,10 +1131,13 @@ static enum libusb_error process_new_device (struct libusb_context *ctx, struct usbi_localize_device_descriptor(&dev->device_descriptor); dev->session_data = cached_device->session; + if (NULL != dev->parent_dev) { + libusb_unref_device(dev->parent_dev); + dev->parent_dev = NULL; + } + if (cached_device->parent_session > 0) { dev->parent_dev = usbi_get_device_by_session_id (ctx, (unsigned long) cached_device->parent_session); - } else { - dev->parent_dev = NULL; } (*(priv->dev->device))->GetDeviceSpeed (priv->dev->device, &devSpeed); diff --git a/libusb/version_nano.h b/libusb/version_nano.h index f375044..1f2431b 100644 --- a/libusb/version_nano.h +++ b/libusb/version_nano.h @@ -1 +1 @@ -#define LIBUSB_NANO 11629 +#define LIBUSB_NANO 11630 -- 2.7.4