The C Source, Patches and (shudder!) Bugs
Post Reply
gsson
Posts: 1
Joined: Tue Mar 24, 2015 4:45 pm

Missing check for libusb_init() return code

Post by gsson » Tue Mar 24, 2015 4:48 pm

If libusb_init() fails, usb_modeswitch will segfault in the libusb_set_debug() call.

Code: Select all

diff -urN usb-modeswitch-2.2.1/usb_modeswitch.c usb-modeswitch-2.2.1p0/usb_modeswitch.c
--- usb-modeswitch-2.2.1/usb_modeswitch.c       2015-01-14 21:13:59.000000000 +0100
+++ usb-modeswitch-2.2.1p0/usb_modeswitch.c     2015-03-24 15:41:57.000000000 +0100
@@ -425,6 +425,7 @@
        int numDefaults=0, sonySuccess=0;
        int currentConfig=0, defaultClass=0, interfaceClass=0;
        struct libusb_device_descriptor descriptor;
+       enum libusb_error libusbError;
 
        /* Make sure we have empty strings even if not set by config */
        TargetProductList[0] = '\0';
@@ -492,7 +493,10 @@
        }
 
        /* libusb initialization */
-       libusb_init(&ctx);
+       if ((libusbError = libusb_init(&ctx)) != LIBUSB_SUCCESS) {
+               fprintf(stderr, "Error: Failed to initialize libusb. %s(%d): %s\n\n", libusb_error_name(libusbError), libusbError, libusb_strerror(libusbError));
+               exit(1);
+       }
 
        if (verbose)
                libusb_set_debug(ctx, 3);

Josh
Site Admin
Posts: 6534
Joined: Sat Nov 03, 2007 12:30 am

Re: Missing check for libusb_init() return code

Post by Josh » Tue Mar 24, 2015 8:02 pm

Thanks, good point, will apply the patch for the next release !!

Post Reply