am7xxx: check the return value of libusb_get_configuration()
authorAntonio Ospite <ao2@ao2.it>
Mon, 20 Apr 2015 15:09:19 +0000 (17:09 +0200)
committerAntonio Ospite <ao2@ao2.it>
Mon, 20 Apr 2015 16:03:55 +0000 (18:03 +0200)
libusb_get_configuration() can still fail after the device has been
opened, so check its actual return value before trusting the returned
current_configuration.

src/am7xxx.c

index 1c3f704..b00a6be 100644 (file)
@@ -734,7 +734,14 @@ static int open_device(am7xxx_context *ctx,
         */
 
        current_configuration = -1;
         */
 
        current_configuration = -1;
-       libusb_get_configuration((*dev)->usb_device, &current_configuration);
+       ret = libusb_get_configuration((*dev)->usb_device,
+                                      &current_configuration);
+       if (ret < 0) {
+               debug(ctx, "libusb_get_configuration failed: %s\n",
+                     libusb_error_name(ret));
+               goto out_libusb_close;
+       }
+
        if (current_configuration != (*dev)->desc->configuration) {
                /*
                 * In principle kernel drivers bound to each interface should
        if (current_configuration != (*dev)->desc->configuration) {
                /*
                 * In principle kernel drivers bound to each interface should
@@ -772,7 +779,14 @@ static int open_device(am7xxx_context *ctx,
         * http://libusb.sourceforge.net/api-1.0/caveats.html
         */
        current_configuration = -1;
         * http://libusb.sourceforge.net/api-1.0/caveats.html
         */
        current_configuration = -1;
-       libusb_get_configuration((*dev)->usb_device, &current_configuration);
+       ret = libusb_get_configuration((*dev)->usb_device,
+                                      &current_configuration);
+       if (ret < 0) {
+               debug(ctx, "libusb_get_configuration after claim failed: %s\n",
+                     libusb_error_name(ret));
+               goto out_libusb_close;
+       }
+
        if (current_configuration != (*dev)->desc->configuration) {
                debug(ctx, "libusb configuration changed (expected: %hhu, current: %hhu\n",
                      (*dev)->desc->configuration, current_configuration);
        if (current_configuration != (*dev)->desc->configuration) {
                debug(ctx, "libusb configuration changed (expected: %hhu, current: %hhu\n",
                      (*dev)->desc->configuration, current_configuration);