Blame SOURCES/0009-core-Do-not-attempt-to-destroy-a-default-context-tha.patch

311d97
From d414a9c447c79b0e39587998f71f8576605b4fb4 Mon Sep 17 00:00:00 2001
311d97
From: Chris Dickens <christopher.a.dickens@gmail.com>
311d97
Date: Mon, 13 Jan 2020 15:05:00 -0800
311d97
Subject: [PATCH 09/10] core: Do not attempt to destroy a default context that
311d97
 doesn't exist
311d97
311d97
Calling libusb_exit(NULL) when a successful call to libusb_init(NULL)
311d97
has not been made results in a segmentation violation. This is
311d97
definitely a user error, but we can easily guard against it.
311d97
311d97
Closes #511
311d97
311d97
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
311d97
(cherry picked from commit a5624b22267ec0e146825d3fe94d9e4b2f5ae503)
311d97
---
311d97
 libusb/core.c         | 16 +++++++++++-----
311d97
 libusb/version_nano.h |  2 +-
311d97
 2 files changed, 12 insertions(+), 6 deletions(-)
311d97
311d97
diff --git a/libusb/core.c b/libusb/core.c
311d97
index 5c0a2e0..26df871 100644
311d97
--- a/libusb/core.c
311d97
+++ b/libusb/core.c
311d97
@@ -2305,7 +2305,7 @@ int API_EXPORTED libusb_init(libusb_context **context)
311d97
 	usbi_mutex_static_lock(&active_contexts_lock);
311d97
 	if (first_init) {
311d97
 		first_init = 0;
311d97
-		list_init (&active_contexts_list);
311d97
+		list_init(&active_contexts_list);
311d97
 	}
311d97
 	list_add (&ctx->list, &active_contexts_list);
311d97
 	usbi_mutex_static_unlock(&active_contexts_lock);
311d97
@@ -2337,7 +2337,7 @@ err_free_ctx:
311d97
 	}
311d97
 
311d97
 	usbi_mutex_static_lock(&active_contexts_lock);
311d97
-	list_del (&ctx->list);
311d97
+	list_del(&ctx->list);
311d97
 	usbi_mutex_static_unlock(&active_contexts_lock);
311d97
 
311d97
 	usbi_mutex_lock(&ctx->usb_devs_lock);
311d97
@@ -2375,6 +2375,12 @@ void API_EXPORTED libusb_exit(struct libusb_context *ctx)
311d97
 	 * if we're the last user */
311d97
 	usbi_mutex_static_lock(&default_context_lock);
311d97
 	if (ctx == usbi_default_context) {
311d97
+		if (!usbi_default_context) {
311d97
+			usbi_dbg("no default context, not initialized?");
311d97
+			usbi_mutex_static_unlock(&default_context_lock);
311d97
+			return;
311d97
+		}
311d97
+
311d97
 		if (--default_context_refcnt > 0) {
311d97
 			usbi_dbg("not destroying default context");
311d97
 			usbi_mutex_static_unlock(&default_context_lock);
311d97
@@ -2390,12 +2396,12 @@ void API_EXPORTED libusb_exit(struct libusb_context *ctx)
311d97
 		 */
311d97
 		destroying_default_context = 1;
311d97
 	} else {
311d97
-		// Unlock default context, as we're not modifying it.
311d97
+		/* Unlock default context, as we're not modifying it. */
311d97
 		usbi_mutex_static_unlock(&default_context_lock);
311d97
-  }
311d97
+	}
311d97
 
311d97
 	usbi_mutex_static_lock(&active_contexts_lock);
311d97
-	list_del (&ctx->list);
311d97
+	list_del(&ctx->list);
311d97
 	usbi_mutex_static_unlock(&active_contexts_lock);
311d97
 
311d97
 	if (libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) {
311d97
diff --git a/libusb/version_nano.h b/libusb/version_nano.h
311d97
index 57c2b1d..3247cec 100644
311d97
--- a/libusb/version_nano.h
311d97
+++ b/libusb/version_nano.h
311d97
@@ -1 +1 @@
311d97
-#define LIBUSB_NANO 11426
311d97
+#define LIBUSB_NANO 11427
311d97
-- 
311d97
2.26.1
311d97