72 lines
2.3 KiB
Diff
72 lines
2.3 KiB
Diff
|
|
From d7f8d239cb49775a1fe7765d6edb463d7fe669e1 Mon Sep 17 00:00:00 2001
|
||
|
|
From: Chris Dickens <christopher.a.dickens@gmail.com>
|
||
|
|
Date: Mon, 13 Jan 2020 15:05:00 -0800
|
||
|
|
Subject: [PATCH 7/8] core: Do not attempt to destroy a default context that
|
||
|
|
doesn't exist
|
||
|
|
|
||
|
|
Calling libusb_exit(NULL) when a successful call to libusb_init(NULL)
|
||
|
|
has not been made results in a segmentation violation. This is
|
||
|
|
definitely a user error, but we can easily guard against it.
|
||
|
|
|
||
|
|
Closes #511
|
||
|
|
|
||
|
|
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
|
||
|
|
---
|
||
|
|
libusb/core.c | 16 +++++++++++-----
|
||
|
|
1 file changed, 11 insertions(+), 5 deletions(-)
|
||
|
|
|
||
|
|
diff --git a/libusb/core.c b/libusb/core.c
|
||
|
|
index 82a5d03..3b6a265 100644
|
||
|
|
--- a/libusb/core.c
|
||
|
|
+++ b/libusb/core.c
|
||
|
|
@@ -2304,7 +2304,7 @@ int API_EXPORTED libusb_init(libusb_context **context)
|
||
|
|
usbi_mutex_static_lock(&active_contexts_lock);
|
||
|
|
if (first_init) {
|
||
|
|
first_init = 0;
|
||
|
|
- list_init (&active_contexts_list);
|
||
|
|
+ list_init(&active_contexts_list);
|
||
|
|
}
|
||
|
|
list_add (&ctx->list, &active_contexts_list);
|
||
|
|
usbi_mutex_static_unlock(&active_contexts_lock);
|
||
|
|
@@ -2336,7 +2336,7 @@ err_free_ctx:
|
||
|
|
}
|
||
|
|
|
||
|
|
usbi_mutex_static_lock(&active_contexts_lock);
|
||
|
|
- list_del (&ctx->list);
|
||
|
|
+ list_del(&ctx->list);
|
||
|
|
usbi_mutex_static_unlock(&active_contexts_lock);
|
||
|
|
|
||
|
|
usbi_mutex_lock(&ctx->usb_devs_lock);
|
||
|
|
@@ -2374,6 +2374,12 @@ void API_EXPORTED libusb_exit(struct libusb_context *ctx)
|
||
|
|
* if we're the last user */
|
||
|
|
usbi_mutex_static_lock(&default_context_lock);
|
||
|
|
if (ctx == usbi_default_context) {
|
||
|
|
+ if (!usbi_default_context) {
|
||
|
|
+ usbi_dbg("no default context, not initialized?");
|
||
|
|
+ usbi_mutex_static_unlock(&default_context_lock);
|
||
|
|
+ return;
|
||
|
|
+ }
|
||
|
|
+
|
||
|
|
if (--default_context_refcnt > 0) {
|
||
|
|
usbi_dbg("not destroying default context");
|
||
|
|
usbi_mutex_static_unlock(&default_context_lock);
|
||
|
|
@@ -2389,12 +2395,12 @@ void API_EXPORTED libusb_exit(struct libusb_context *ctx)
|
||
|
|
*/
|
||
|
|
destroying_default_context = 1;
|
||
|
|
} else {
|
||
|
|
- // Unlock default context, as we're not modifying it.
|
||
|
|
+ /* Unlock default context, as we're not modifying it. */
|
||
|
|
usbi_mutex_static_unlock(&default_context_lock);
|
||
|
|
- }
|
||
|
|
+ }
|
||
|
|
|
||
|
|
usbi_mutex_static_lock(&active_contexts_lock);
|
||
|
|
- list_del (&ctx->list);
|
||
|
|
+ list_del(&ctx->list);
|
||
|
|
usbi_mutex_static_unlock(&active_contexts_lock);
|
||
|
|
|
||
|
|
if (libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) {
|
||
|
|
--
|
||
|
|
2.27.0.windows.1
|
||
|
|
|