darwin: Fix crash in log handler when stopping event thread

The darwin event thread (in contrast to other OS implementations) tries
to log to the context that created it. However, this context is only
guaranteed to be valid until the thread has started. It may be that
another context is the last one to be destroyed, causing the event
thread to log using an already destroyed context.

Fix this by only passing on ctx where it is acceptable.

Fixes #1108
This commit is contained in:
Benjamin Berg
2022-04-07 12:43:08 +02:00
committed by Tormod Volden
parent 32f3c6db95
commit ff05357a83
2 changed files with 5 additions and 4 deletions

View File

@@ -494,6 +494,7 @@ static void *darwin_event_thread_main (void *arg0) {
io_iterator_t libusb_rem_device_iterator;
io_iterator_t libusb_add_device_iterator;
/* ctx must only be used for logging during thread startup */
usbi_dbg (ctx, "creating hotplug event source");
runloop = CFRunLoopGetCurrent ();
@@ -515,7 +516,7 @@ static void *darwin_event_thread_main (void *arg0) {
kresult = IOServiceAddMatchingNotification (libusb_notification_port, kIOTerminatedNotification,
IOServiceMatching(darwin_device_class),
darwin_devices_detached,
ctx, &libusb_rem_device_iterator);
NULL, &libusb_rem_device_iterator);
if (kresult != kIOReturnSuccess) {
usbi_err (ctx, "could not add hotplug event source: %s", darwin_error_str (kresult));
@@ -528,7 +529,7 @@ static void *darwin_event_thread_main (void *arg0) {
kresult = IOServiceAddMatchingNotification(libusb_notification_port, kIOFirstMatchNotification,
IOServiceMatching(darwin_device_class),
darwin_devices_attached,
ctx, &libusb_add_device_iterator);
NULL, &libusb_add_device_iterator);
if (kresult != kIOReturnSuccess) {
usbi_err (ctx, "could not add hotplug event source: %s", darwin_error_str (kresult));
@@ -553,7 +554,7 @@ static void *darwin_event_thread_main (void *arg0) {
/* run the runloop */
CFRunLoopRun();
usbi_dbg (ctx, "darwin event thread exiting");
usbi_dbg (NULL, "darwin event thread exiting");
/* signal the main thread that the hotplug runloop has finished. */
pthread_mutex_lock (&libusb_darwin_at_mutex);

View File

@@ -1 +1 @@
#define LIBUSB_NANO 11720
#define LIBUSB_NANO 11722