diff --git a/drivers/usb/core/config.c b/drivers/usb/core/config.c index 7008ba6a7be3f1..1bdf8bd0eb1b84 100644 --- a/drivers/usb/core/config.c +++ b/drivers/usb/core/config.c @@ -920,10 +920,12 @@ int usb_get_bos_descriptor(struct usb_device *dev) for (i = 0; i < num; i++) { buffer += length; cap = (struct usb_dev_cap_header *)buffer; - length = cap->bLength; - if (total_len < length) + if (total_len < sizeof(*cap) || total_len < cap->bLength) { + dev->bos->desc->bNumDeviceCaps = i; break; + } + length = cap->bLength; total_len -= length; if (cap->bDescriptorType != USB_DT_DEVICE_CAPABILITY) {