diff options
author | Sean McBride <sean@rogue-research.com> | 2012-10-12 17:31:45 -0400 |
---|---|---|
committer | Ludovic Rousseau <ludovic.rousseau+github@gmail.com> | 2013-08-02 11:30:16 +0200 |
commit | fedc3631f88462d7dfa6af7fc1328b5675eea059 (patch) | |
tree | c5cac2e76ea85fb298f198a1c557ccdeefaa0b13 | |
parent | b5acea3591ee713ec3392e8d3c8ba59e848d8641 (diff) | |
download | android_external_libusbx-fedc3631f88462d7dfa6af7fc1328b5675eea059.tar.gz android_external_libusbx-fedc3631f88462d7dfa6af7fc1328b5675eea059.tar.bz2 android_external_libusbx-fedc3631f88462d7dfa6af7fc1328b5675eea059.zip |
Core: defensive programming
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.
-rw-r--r-- | libusb/descriptor.c | 6 | ||||
-rw-r--r-- | libusb/version_nano.h | 2 |
2 files changed, 5 insertions, 3 deletions
diff --git a/libusb/descriptor.c b/libusb/descriptor.c index ba6d146..e1c4915 100644 --- a/libusb/descriptor.c +++ b/libusb/descriptor.c @@ -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; diff --git a/libusb/version_nano.h b/libusb/version_nano.h index 7176abc..13fec84 100644 --- a/libusb/version_nano.h +++ b/libusb/version_nano.h @@ -1 +1 @@ -#define LIBUSB_NANO 10797 +#define LIBUSB_NANO 10798 |