Windows: Fix memory and handle leaks in winusbx_claim_interface()
authorChris Dickens <christopher.a.dickens@gmail.com>
Thu, 12 Jan 2017 22:36:20 +0000 (14:36 -0800)
committerChris Dickens <christopher.a.dickens@gmail.com>
Thu, 12 Jan 2017 23:26:40 +0000 (15:26 -0800)
When using the libusb0 filter driver, the loop could potentially leak the
memory for dev_interface_details. In that same loop, if the dev path
without the GUID matches the interface path, the new file handle would
be leaked.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
libusb/os/windows_winusb.c
libusb/version_nano.h

index 3bb5414..94e6bca 100644 (file)
@@ -2573,26 +2573,35 @@ static int winusbx_claim_interface(int sub_api, struct libusb_device_handle *dev
                                for (i = 0; ; i++) {
                                        safe_free(dev_interface_details);
                                        safe_free(dev_path_no_guid);
+
                                        dev_interface_details = get_interface_details_filter(ctx, &dev_info, &dev_info_data, &GUID_DEVINTERFACE_LIBUSB0_FILTER, i, filter_path);
                                        if ((found_filter) || (dev_interface_details == NULL))
                                                break;
 
                                        // ignore GUID part
                                        dev_path_no_guid = sanitize_path(strtok(dev_interface_details->DevicePath, "{"));
-                                       if (safe_strncmp(dev_path_no_guid, priv->usb_interface[iface].path, safe_strlen(dev_path_no_guid)) == 0) {
+                                       if (dev_path_no_guid == NULL)
+                                               continue;
+
+                                       if (strncmp(dev_path_no_guid, priv->usb_interface[iface].path, strlen(dev_path_no_guid)) == 0) {
                                                file_handle = CreateFileA(filter_path, GENERIC_WRITE | GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ,
                                                        NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
-                                               if (file_handle == INVALID_HANDLE_VALUE) {
-                                                       usbi_err(ctx, "could not open device %s: %s", filter_path, windows_error_str(0));
-                                               } else {
-                                                       WinUSBX[sub_api].Free(winusb_handle);
-                                                       if (WinUSBX[sub_api].Initialize(file_handle, &winusb_handle))
+                                               if (file_handle != INVALID_HANDLE_VALUE) {
+                                                       if (WinUSBX[sub_api].Initialize(file_handle, &winusb_handle)) {
+                                                               // Replace the existing file handle with the working one
+                                                               CloseHandle(handle_priv->interface_handle[iface].dev_handle);
+                                                               handle_priv->interface_handle[iface].dev_handle = file_handle;
                                                                found_filter = true;
-                                                       else
+                                                       } else {
                                                                usbi_err(ctx, "could not initialize filter driver for %s", filter_path);
+                                                               CloseHandle(file_handle);
+                                                       }
+                                               } else {
+                                                       usbi_err(ctx, "could not open device %s: %s", filter_path, windows_error_str(0));
                                                }
                                        }
                                }
+                               free(dev_interface_details);
                                if (!found_filter) {
                                        usbi_err(ctx, "could not access interface %d: %s", iface, windows_error_str(err));
                                        return LIBUSB_ERROR_ACCESS;
index 2ede58a..6078134 100644 (file)
@@ -1 +1 @@
-#define LIBUSB_NANO 11177
+#define LIBUSB_NANO 11178