Skip to content

Commit 9eaebb7

Browse files
committed
darwin: do not set NULL device in darwin_reload_device
We need to maintain the invariant that `device` is never an invalid pointer because another thread might be concurrently accessing it. We want that thread to error from an outdated `device` rather than crash on a NULL pointer.
1 parent 80a7d8e commit 9eaebb7

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

libusb/os/darwin_usb.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2540,13 +2540,15 @@ static bool darwin_has_capture_entitlements (void) {
25402540
static int darwin_reload_device (struct libusb_device_handle *dev_handle) {
25412541
struct darwin_cached_device *dpriv = DARWIN_CACHED_DEVICE(dev_handle->dev);
25422542
enum libusb_error err;
2543+
usb_device_t **new_device;
25432544

25442545
usbi_mutex_lock(&darwin_cached_devices_mutex);
2545-
(*(dpriv->device))->Release(dpriv->device);
2546-
dpriv->device = darwin_device_from_service (HANDLE_CTX (dev_handle), dpriv->service);
2547-
if (!dpriv->device) {
2546+
new_device = darwin_device_from_service (HANDLE_CTX (dev_handle), dpriv->service);
2547+
if (!new_device) {
25482548
err = LIBUSB_ERROR_NO_DEVICE;
25492549
} else {
2550+
(*(dpriv->device))->Release(dpriv->device);
2551+
dpriv->device = new_device;
25502552
err = LIBUSB_SUCCESS;
25512553
}
25522554
usbi_mutex_unlock(&darwin_cached_devices_mutex);

0 commit comments

Comments
 (0)