Blob Blame History Raw
diff -up hplip-3.15.9/io/hpmud/musb.c.hplip-usb-no-crash hplip-3.15.9/io/hpmud/musb.c
--- hplip-3.15.9/io/hpmud/musb.c.hplip-usb-no-crash	2016-06-10 12:51:45.981782728 +0200
+++ hplip-3.15.9/io/hpmud/musb.c	2016-06-10 13:07:57.691178754 +0200
@@ -691,7 +691,8 @@ static libusb_device *get_libusb_device(
     int numdevs = 0;        /* number of connected devices */
     int i, conf, iface, altset ;
 
-    libusb_init(&libusb_ctx);
+    i = libusb_init(&libusb_ctx);
+    if (i) goto bugout;
     numdevs = libusb_get_device_list(libusb_ctx, &libusb_dev_list);
     for (i=0; i< numdevs; i++)
     {
@@ -2022,7 +2023,7 @@ bugout:
 int __attribute__ ((visibility ("hidden"))) musb_probe_devices(char *lst, int lst_size, int *cnt)
 {
     libusb_context *ctx = NULL;
-    libusb_device **list; /*List of connected USB devices */
+    libusb_device **list = NULL; /*List of connected USB devices */
     libusb_device *dev = NULL; /* Current device */
     struct libusb_device_descriptor devdesc; /* Current device descriptor */
     struct libusb_config_descriptor *confptr = NULL; /* Pointer to current configuration */
@@ -2039,7 +2040,8 @@ int __attribute__ ((visibility ("hidden"
     char serial[128], mfg[128], sz[HPMUD_LINE_SIZE];
     int r, size=0;
 
-    libusb_init(&ctx);
+    i = libusb_init(&ctx);
+    if (i) goto bugout;
     numdevs = libusb_get_device_list(ctx, &list);
 
     if (numdevs <= 0)
@@ -2135,12 +2137,14 @@ int __attribute__ ((visibility ("hidden"
     }//end for loop
 
 bugout:
-    if (!hd)
+    if (hd)
         libusb_close(hd);
     if (confptr)
         libusb_free_config_descriptor(confptr);
-    libusb_free_device_list(list, 1);
-    libusb_exit(ctx);
+    if (list)
+        libusb_free_device_list(list, 1);
+    if (ctx)
+        libusb_exit(ctx);
 
     return size;
 }
@@ -2148,7 +2152,7 @@ bugout:
 enum HPMUD_RESULT hpmud_make_usb_uri(const char *busnum, const char *devnum, char *uri, int uri_size, int *bytes_read)
 {
     libusb_context *ctx = NULL;
-    libusb_device **list; /*List of connected USB devices */
+    libusb_device **list = NULL; /*List of connected USB devices */
     libusb_device *dev = NULL, *found_dev=NULL;
     libusb_device_handle *hd=NULL;
     struct libusb_device_descriptor devdesc; /* Current device descriptor */
@@ -2166,7 +2170,8 @@ enum HPMUD_RESULT hpmud_make_usb_uri(con
 
     *bytes_read=0;
 
-    libusb_init(&ctx);
+    i = libusb_init(&ctx);
+    if (i) goto bugout;
     numdevs = libusb_get_device_list(ctx, &list);
 
     if (numdevs <= 0)
@@ -2269,8 +2274,10 @@ bugout:
     if (hd != NULL)
         libusb_close(hd);
 
-    libusb_free_device_list(list, 1);
-    libusb_exit(ctx);
+    if (list)
+        libusb_free_device_list(list, 1);
+    if (ctx)
+        libusb_exit(ctx);
 
     return stat;
 }
@@ -2278,7 +2285,7 @@ bugout:
 enum HPMUD_RESULT hpmud_make_usb_serial_uri(const char *sn, char *uri, int uri_size, int *bytes_read)
 {
     libusb_context *ctx = NULL;
-    libusb_device **list; /*List of connected USB devices */
+    libusb_device **list = NULL; /*List of connected USB devices */
     libusb_device *dev = NULL, *found_dev=NULL;
 
     char model[128];
@@ -2289,7 +2296,8 @@ enum HPMUD_RESULT hpmud_make_usb_serial_
 
     *bytes_read=0;
 
-    libusb_init(&ctx);
+    i = libusb_init(&ctx);
+    if (i) goto bugout;
     numdevs = libusb_get_device_list(ctx, &list);
 
     if (numdevs <= 0)
@@ -2315,8 +2323,10 @@ enum HPMUD_RESULT hpmud_make_usb_serial_
     stat = HPMUD_R_OK;
 
 bugout:
-    libusb_free_device_list(list, 1);
-    libusb_exit(ctx);
+    if (list)
+        libusb_free_device_list(list, 1);
+    if (ctx)
+        libusb_exit(ctx);
 
     return stat;
 }