From bbe25fac47c2dfd2d50a2c3a1b0ed96473d8dc1c Mon Sep 17 00:00:00 2001 From: Hans de Goede Date: Thu, 23 May 2013 14:40:05 +0200 Subject: [PATCH] all: Make usbi_os_backend.get_[active_]config_descriptor return len Since commit 5e479f1821d3294fb1cc70c5867c69eca2551de7: "Core: Avoid short read failures on broken descriptors" usbi_os_backend.get_[active_]config_descriptor no longer return on error (under Linux) when returning less bytes then requested. But libusb_get_[active_]config_descriptor still not only requests wTotalLength bytes, but also blindly assumes that on success it has gotten wTotalLength bytes. This patch fixes this, it changes all usbi_os_backend.get_*config_descriptor implementations to return the actual length on success and uses this value as the descriptor size in parse_configuration(). Note that the linux and wince backends were already returning the actual length and thus are not touched. Signed-off-by: Hans de Goede --- libusb/descriptor.c | 8 +++----- libusb/os/darwin_usb.c | 7 ++++++- libusb/os/openbsd_usb.c | 4 ++-- libusb/os/windows_usb.c | 2 +- libusb/version_nano.h | 2 +- 5 files changed, 13 insertions(+), 10 deletions(-) diff --git a/libusb/descriptor.c b/libusb/descriptor.c index 9047352..74d3365 100644 --- a/libusb/descriptor.c +++ b/libusb/descriptor.c @@ -338,17 +338,15 @@ static void clear_configuration(struct libusb_config_descriptor *config) static int parse_configuration(struct libusb_context *ctx, struct libusb_config_descriptor *config, unsigned char *buffer, - int host_endian) + int size, int host_endian) { int i; int r; - int size; size_t tmp; struct usb_descriptor_header header; struct libusb_interface *usb_interface; usbi_parse_descriptor(buffer, "bbwbbbbb", config, host_endian); - size = config->wTotalLength; if (config->bNumInterfaces > USB_MAXINTERFACES) { usbi_err(ctx, "too many interfaces (%d)", config->bNumInterfaces); @@ -521,7 +519,7 @@ int API_EXPORTED libusb_get_active_config_descriptor(libusb_device *dev, if (r < 0) goto err; - r = parse_configuration(dev->ctx, _config, buf, host_endian); + r = parse_configuration(dev->ctx, _config, buf, r, host_endian); if (r < 0) { usbi_err(dev->ctx, "parse_configuration failed with error %d", r); goto err; @@ -591,7 +589,7 @@ int API_EXPORTED libusb_get_config_descriptor(libusb_device *dev, if (r < 0) goto err; - r = parse_configuration(dev->ctx, _config, buf, host_endian); + r = parse_configuration(dev->ctx, _config, buf, r, host_endian); if (r < 0) { usbi_err(dev->ctx, "parse_configuration failed with error %d", r); goto err; diff --git a/libusb/os/darwin_usb.c b/libusb/os/darwin_usb.c index 74e34cf..f9bd479 100644 --- a/libusb/os/darwin_usb.c +++ b/libusb/os/darwin_usb.c @@ -515,6 +515,7 @@ static int darwin_get_config_descriptor(struct libusb_device *dev, uint8_t confi struct darwin_device_priv *priv = (struct darwin_device_priv *)dev->os_priv; IOUSBConfigurationDescriptorPtr desc; IOReturn kresult; + int ret; if (!priv || !priv->device) return LIBUSB_ERROR_OTHER; @@ -531,7 +532,11 @@ static int darwin_get_config_descriptor(struct libusb_device *dev, uint8_t confi *host_endian = 0; } - return darwin_to_libusb (kresult); + ret = darwin_to_libusb (kresult); + if (ret != LIBUSB_SUCCESS) + return ret; + + return len; } /* check whether the os has configured the device */ diff --git a/libusb/os/openbsd_usb.c b/libusb/os/openbsd_usb.c index 353385b..06aaff4 100644 --- a/libusb/os/openbsd_usb.c +++ b/libusb/os/openbsd_usb.c @@ -269,7 +269,7 @@ obsd_get_active_config_descriptor(struct libusb_device *dev, *host_endian = 0; - return (LIBUSB_SUCCESS); + return len; } int @@ -307,7 +307,7 @@ obsd_get_config_descriptor(struct libusb_device *dev, uint8_t idx, *host_endian = 0; - return (LIBUSB_SUCCESS); + return len; } int diff --git a/libusb/os/windows_usb.c b/libusb/os/windows_usb.c index b07d193..061a0f8 100644 --- a/libusb/os/windows_usb.c +++ b/libusb/os/windows_usb.c @@ -1758,7 +1758,7 @@ static int windows_get_config_descriptor(struct libusb_device *dev, uint8_t conf size = min(config_header->wTotalLength, len); memcpy(buffer, priv->config_descriptor[config_index], size); - return LIBUSB_SUCCESS; + return size; } /* diff --git a/libusb/version_nano.h b/libusb/version_nano.h index 0efafb3..8f8d647 100644 --- a/libusb/version_nano.h +++ b/libusb/version_nano.h @@ -1 +1 @@ -#define LIBUSB_NANO 10709 +#define LIBUSB_NANO 10710 -- 2.11.4.GIT