summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Dickens <christopher.a.dickens@gmail.com>2016-01-27 11:46:01 -0800
committerChris Dickens <christopher.a.dickens@gmail.com>2016-01-27 11:46:01 -0800
commitc8f71151464141bf515353e5c2aff9e56bc15657 (patch)
tree41a2d1bb6b8ef94ba96f841350019bdb2c1c44dd
parent4dfa6d6493ea52b81f4ebccfa5a1214746f58b6c (diff)
downloadlibusb-c8f71151464141bf515353e5c2aff9e56bc15657.tar.gz
Windows: Ensure proper cleanup when backend init() functions fail
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
-rw-r--r--libusb/os/windows_usbdk.c30
-rw-r--r--libusb/os/windows_winusb.c12
-rw-r--r--libusb/version_nano.h2
3 files changed, 29 insertions, 15 deletions
diff --git a/libusb/os/windows_usbdk.c b/libusb/os/windows_usbdk.c
index 2bf3d80..da12eda 100644
--- a/libusb/os/windows_usbdk.c
+++ b/libusb/os/windows_usbdk.c
@@ -114,16 +114,18 @@ static FARPROC get_usbdk_proc_addr(struct libusb_context *ctx, LPCSTR api_name)
{
FARPROC api_ptr = GetProcAddress(usbdk_helper.module, api_name);
- if (api_ptr == NULL) {
+ if (api_ptr == NULL)
usbi_err(ctx, "UsbDkHelper API %s not found, error %d", api_name, GetLastError());
- }
return api_ptr;
}
static void unload_usbdk_helper_dll(void)
{
- FreeLibrary(usbdk_helper.module);
+ if (usbdk_helper.module != NULL) {
+ FreeLibrary(usbdk_helper.module);
+ usbdk_helper.module = NULL;
+ }
}
static int load_usbdk_helper_dll(struct libusb_context *ctx)
@@ -190,6 +192,7 @@ static int load_usbdk_helper_dll(struct libusb_context *ctx)
error_unload:
FreeLibrary(usbdk_helper.module);
+ usbdk_helper.module = NULL;
return LIBUSB_ERROR_NOT_FOUND;
}
@@ -197,23 +200,30 @@ static int usbdk_init(struct libusb_context *ctx)
{
int r;
- if (++concurrent_usage == 0) {
+ if (++concurrent_usage == 0) { // First init?
r = load_usbdk_helper_dll(ctx);
if (r)
- return r;
+ goto init_exit;
init_polling();
r = windows_common_init(ctx);
if (r)
- goto error_roll_back;
+ goto init_exit;
}
+ // At this stage, either we went through full init successfully, or didn't need to
+ r = LIBUSB_SUCCESS;
- return LIBUSB_SUCCESS;
+init_exit:
+ if (!concurrent_usage && r != LIBUSB_SUCCESS) { // First init failed?
+ exit_polling();
+ windows_common_exit();
+ unload_usbdk_helper_dll();
+ }
+
+ if (r != LIBUSB_SUCCESS)
+ --concurrent_usage; // Not expected to call libusb_exit if we failed.
-error_roll_back:
- windows_common_exit();
- unload_usbdk_helper_dll();
return r;
}
diff --git a/libusb/os/windows_winusb.c b/libusb/os/windows_winusb.c
index 8670be7..5bf35a2 100644
--- a/libusb/os/windows_winusb.c
+++ b/libusb/os/windows_winusb.c
@@ -760,9 +760,9 @@ static int windows_init(struct libusb_context *ctx)
{
int i, r = LIBUSB_ERROR_OTHER;
HANDLE semaphore;
- char sem_name[11 + 1 + 8]; // strlen(libusb_init)+'\0'+(32-bit hex PID)
+ char sem_name[11 + 8 + 1]; // strlen("libusb_init") + (32-bit hex PID) + '\0'
- sprintf(sem_name, "libusb_init%08X", (unsigned int)GetCurrentProcessId());
+ sprintf(sem_name, "libusb_init%08X", (unsigned int)(GetCurrentProcessId() & 0xFFFFFFFF));
semaphore = CreateSemaphoreA(NULL, 1, 1, sem_name);
if (semaphore == NULL) {
usbi_err(ctx, "could not create semaphore: %s", windows_error_str(0));
@@ -782,6 +782,7 @@ static int windows_init(struct libusb_context *ctx)
if (++concurrent_usage == 0) { // First init?
get_windows_version();
usbi_dbg(windows_version_str);
+
if (windows_version == WINDOWS_UNSUPPORTED) {
usbi_err(ctx, "This version of Windows is NOT supported");
r = LIBUSB_ERROR_NOT_SUPPORTED;
@@ -813,6 +814,9 @@ static int windows_init(struct libusb_context *ctx)
init_exit: // Holds semaphore here.
if (!concurrent_usage && r != LIBUSB_SUCCESS) { // First init failed?
+ for (i = 0; i < USB_API_MAX; i++)
+ usb_api_backend[i].exit(SUBAPI_NOT_SET);
+ exit_polling();
windows_common_exit();
usbi_mutex_destroy(&autoclaim_lock);
}
@@ -1622,9 +1626,9 @@ static void windows_exit(void)
{
int i;
HANDLE semaphore;
- char sem_name[11 + 1 + 8]; // strlen(libusb_init)+'\0'+(32-bit hex PID)
+ char sem_name[11 + 8 + 1]; // strlen("libusb_init") + (32-bit hex PID) + '\0'
- sprintf(sem_name, "libusb_init%08X", (unsigned int)GetCurrentProcessId());
+ sprintf(sem_name, "libusb_init%08X", (unsigned int)(GetCurrentProcessId() & 0xFFFFFFFF));
semaphore = CreateSemaphoreA(NULL, 1, 1, sem_name);
if (semaphore == NULL)
return;
diff --git a/libusb/version_nano.h b/libusb/version_nano.h
index 14860a8..91cf072 100644
--- a/libusb/version_nano.h
+++ b/libusb/version_nano.h
@@ -1 +1 @@
-#define LIBUSB_NANO 11040
+#define LIBUSB_NANO 11041