From a682a050a3160eb0bbc35c833b2d306d20fa8df5 Mon Sep 17 00:00:00 2001 From: x0rloser Date: Thu, 12 Sep 2024 23:03:54 +1000 Subject: [PATCH] Handle descriptors that are longer than defined in the specification. Follow specification guidelines for doing so. --- src/usb.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/usb.rs b/src/usb.rs index 6ae78ab9..05d74ca3 100644 --- a/src/usb.rs +++ b/src/usb.rs @@ -769,11 +769,14 @@ impl Iterator for DescriptorIterator<'_> { let desc_length = remaining_bytes[0] as usize; let desc_type = DescriptorType::from(remaining_bytes[1]); self.offset += desc_length; + // The expected length is the minimum length, but sometimes the + // descriptors have extra padding/garbage data at the end. + // Handle this by trimming off the extra data. if let Some(expected) = desc_type.expected_length() { - if desc_length != expected { + if desc_length < expected { continue } - let bytes = &remaining_bytes[0 .. desc_length]; + let bytes = &remaining_bytes[0 .. expected]; return Some(match desc_type { DescriptorType::Device => Descriptor::Device(