From fedc3631f88462d7dfa6af7fc1328b5675eea059 Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Fri, 12 Oct 2012 17:31:45 -0400 Subject: 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. --- libusb/descriptor.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'libusb/descriptor.c') 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; -- cgit v1.2.1