Core: defensive programming
authorSean McBride <sean@rogue-research.com>
Fri, 12 Oct 2012 21:31:45 +0000 (17:31 -0400)
committerLudovic Rousseau <ludovic.rousseau+github@gmail.com>
Fri, 2 Aug 2013 09:30:16 +0000 (11:30 +0200)
Defensively set return-by-reference value to -1 in error condition

NB: The comments do not match the implementation.

Comments: "[return] the index of the configuration matching a specific
bConfigurationValue in the idx output parameter, or -1 if the config was
not found"

There is a code path where idx is never touched. Perhaps clients of the
function are careful to only read idx if the return value is success,
but also setting idx to -1 is much safer.

libusb/descriptor.c
libusb/version_nano.h

index ba6d146..e1c4915 100644 (file)
@@ -660,7 +660,7 @@ int API_EXPORTED libusb_get_config_descriptor(libusb_device *dev,
 /* iterate through all configurations, returning the index of the configuration
  * matching a specific bConfigurationValue in the idx output parameter, or -1
  * if the config was not found.
- * returns 0 or a LIBUSB_ERROR code
+ * returns 0 on success or a LIBUSB_ERROR code
  */
 int usbi_get_config_index_by_value(struct libusb_device *dev,
        uint8_t bConfigurationValue, int *idx)
@@ -673,8 +673,10 @@ int usbi_get_config_index_by_value(struct libusb_device *dev,
                int host_endian;
                int r = usbi_backend->get_config_descriptor(dev, i, tmp, sizeof(tmp),
                        &host_endian);
-               if (r < 0)
+               if (r < 0) {
+                       *idx = -1;
                        return r;
+               }
                if (tmp[5] == bConfigurationValue) {
                        *idx = i;
                        return 0;
index 7176abc..13fec84 100644 (file)
@@ -1 +1 @@
-#define LIBUSB_NANO 10797
+#define LIBUSB_NANO 10798