From f7084fea1107625fe5aeb275a2a4801bed2920d5 Mon Sep 17 00:00:00 2001 From: Bruno Harbulot Date: Thu, 12 Aug 2021 20:26:12 +0100 Subject: [PATCH] core: Apply default options to all new contexts The default options configured with libusb_set_option(NULL, ...) were only applied when the default context was created, and were ignored when calling libusb_init() with a non-null context pointer. Make sure the default options will be applied to all new contexts. This is important when using LIBUSB_OPTION_NO_DEVICE_DISCOVERY which must be respected during the context initialization in order to work. Closes #942 [Tormod: amend comments] Signed-off-by: Tormod Volden --- libusb/core.c | 16 +++++++++------- libusb/version_nano.h | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/libusb/core.c b/libusb/core.c index 3d2f2b4..e6dca8b 100644 --- a/libusb/core.c +++ b/libusb/core.c @@ -2316,12 +2316,8 @@ int API_EXPORTED libusb_init(libusb_context **ctx) list_init(&_ctx->usb_devs); list_init(&_ctx->open_devs); - /* default context should be initialized before calling usbi_dbg */ - if (!ctx) { - usbi_default_context = _ctx; - default_context_refcnt = 1; - usbi_dbg(usbi_default_context, "created default context"); - + /* apply options to new contexts (also default context being created) */ + if (ctx || !usbi_default_context) { for (enum libusb_option option = 0 ; option < LIBUSB_OPTION_MAX ; option++) { if (LIBUSB_OPTION_LOG_LEVEL == option || !default_context_options[option].is_set) { continue; @@ -2332,6 +2328,13 @@ int API_EXPORTED libusb_init(libusb_context **ctx) } } + /* default context must be initialized before calling usbi_dbg */ + if (!ctx) { + usbi_default_context = _ctx; + default_context_refcnt = 1; + usbi_dbg(usbi_default_context, "created default context"); + } + usbi_dbg(_ctx, "libusb v%u.%u.%u.%u%s", libusb_version_internal.major, libusb_version_internal.minor, libusb_version_internal.micro, libusb_version_internal.nano, libusb_version_internal.rc); @@ -2353,7 +2356,6 @@ int API_EXPORTED libusb_init(libusb_context **ctx) goto err_io_exit; } - if (ctx) *ctx = _ctx; diff --git a/libusb/version_nano.h b/libusb/version_nano.h index 9a3d393..af4599a 100644 --- a/libusb/version_nano.h +++ b/libusb/version_nano.h @@ -1 +1 @@ -#define LIBUSB_NANO 11666 +#define LIBUSB_NANO 11667 -- 2.7.4