Skip to content

Commit d34bbb2

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 952b2ce commit d34bbb2

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

libusb/os/darwin_usb.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2857,10 +2857,14 @@ static bool darwin_has_capture_entitlements (void) {
28572857
static int darwin_reload_device (struct libusb_device_handle *dev_handle) {
28582858
struct darwin_cached_device *dpriv = DARWIN_CACHED_DEVICE(dev_handle->dev);
28592859
enum libusb_error err;
2860+
usb_device_t **new_device;
28602861

28612862
usbi_mutex_lock(&darwin_cached_devices_mutex);
2862-
(*dpriv->device)->Release(dpriv->device);
2863-
err = darwin_device_from_service (HANDLE_CTX (dev_handle), dpriv->service, &dpriv->device);
2863+
err = darwin_device_from_service (HANDLE_CTX (dev_handle), dpriv->service, &new_device);
2864+
if (err == LIBUSB_SUCCESS) {
2865+
(*dpriv->device)->Release(dpriv->device);
2866+
dpriv->device = new_device;
2867+
}
28642868
usbi_mutex_unlock(&darwin_cached_devices_mutex);
28652869

28662870
return err;

0 commit comments

Comments
 (0)