Windows: Fix possible memory leak when caching descriptors fails
authorChris Dickens <christopher.a.dickens@gmail.com>
Mon, 27 Mar 2017 03:37:41 +0000 (20:37 -0700)
committerChris Dickens <christopher.a.dickens@gmail.com>
Mon, 27 Mar 2017 03:37:41 +0000 (20:37 -0700)
Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
libusb/os/windows_winusb.c
libusb/os/windows_winusb.h
libusb/version_nano.h

index bccedb9..2371096 100644 (file)
@@ -974,6 +974,16 @@ static int cache_config_descriptors(struct libusb_device *dev, HANDLE hub_handle
                        LOOP_BREAK(LIBUSB_ERROR_NO_MEM);
                memcpy(priv->config_descriptor[i], cd_data, cd_data->wTotalLength);
        }
+
+       // Any failure will result in dev->num_configurations being forced to 0.
+       // We need to release any memory that may have been allocated for config
+       // descriptors that were successfully retrieved, otherwise that memory
+       // will be leaked
+       if (r != LIBUSB_SUCCESS) {
+               for (i = 0; i < dev->num_configurations; i++)
+                       free(priv->config_descriptor[i]);
+       }
+
        return r;
 }
 
index b7b9cd9..1c8ee34 100644 (file)
@@ -244,10 +244,8 @@ static inline void windows_device_priv_release(struct libusb_device *dev)
        int i;
 
        free(p->path);
-       if ((dev->num_configurations > 0) && (p->config_descriptor != NULL)) {
-               for (i = 0; i < dev->num_configurations; i++)
-                       free(p->config_descriptor[i]);
-       }
+       for (i = 0; i < dev->num_configurations; i++)
+               free(p->config_descriptor[i]);
        free(p->config_descriptor);
        free(p->hid);
        for (i = 0; i < USB_MAXINTERFACES; i++) {
index f75412b..e2bf2ec 100644 (file)
@@ -1 +1 @@
-#define LIBUSB_NANO 11195
+#define LIBUSB_NANO 11196