From 5e8e36e4e3328c5f5a2753b0fac633b2d0ebd0ff Mon Sep 17 00:00:00 2001 From: alufers Date: Tue, 5 Mar 2024 15:41:32 +0100 Subject: [PATCH] fix: panic when trying to iterate over an interface with zero endpoints Some USB classes (UVC, UAC) provide alternate setting descriptors of interfaces with zero endpoints. Trying to call `endpoint_descriptors` on them caused a panic, because NULL was passed to `slice::from_raw_parts`. A branch was introduced to check for that case and create an iterator over an empty slice. See: https://stackoverflow.com/a/49244874/2948315 --- src/interface_descriptor.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/interface_descriptor.rs b/src/interface_descriptor.rs index eb6419f..d4c64c3 100644 --- a/src/interface_descriptor.rs +++ b/src/interface_descriptor.rs @@ -101,11 +101,9 @@ impl<'a> InterfaceDescriptor<'a> { /// Returns an iterator over the interface's endpoint descriptors. pub fn endpoint_descriptors(&self) -> EndpointDescriptors<'a> { - let endpoints = unsafe { - slice::from_raw_parts( - self.descriptor.endpoint, - self.descriptor.bNumEndpoints as usize, - ) + let endpoints = match self.descriptor.bNumEndpoints { + 0 => &[], + n => unsafe { slice::from_raw_parts(self.descriptor.endpoint, n as usize) }, }; EndpointDescriptors {