Skip to content

Commit edafa99

Browse files
committed
Updating PR after review
1 parent 390ac1d commit edafa99

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

src/device_builder.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ pub enum BuilderError {
3333
InvalidPacketSize,
3434
/// Configuration specifies higher USB power draw than allowed
3535
PowerTooHigh,
36+
/// The provided control buffer is too small for the provided maximum packet size.
37+
ControlBufferTooSmall,
3638
}
3739

3840
/// Provides basic string descriptors about the device, including the manufacturer, product name,
@@ -110,8 +112,16 @@ impl<'a, B: UsbBus> UsbDeviceBuilder<'a, B> {
110112
}
111113

112114
/// Creates the [`UsbDevice`] instance with the configuration in this builder.
113-
pub fn build(self) -> UsbDevice<'a, B> {
114-
UsbDevice::build(self.alloc, self.config, self.control_buffer)
115+
pub fn build(self) -> Result<UsbDevice<'a, B>, BuilderError> {
116+
if self.control_buffer.len() < self.config.max_packet_size_0 as usize {
117+
return Err(BuilderError::ControlBufferTooSmall);
118+
}
119+
120+
Ok(UsbDevice::build(
121+
self.alloc,
122+
self.config,
123+
self.control_buffer,
124+
))
115125
}
116126

117127
builder_fields! {
@@ -194,6 +204,10 @@ impl<'a, B: UsbBus> UsbDeviceBuilder<'a, B> {
194204
_ => return Err(BuilderError::InvalidPacketSize),
195205
}
196206

207+
if self.control_buffer.len() < max_packet_size_0 as usize {
208+
return Err(BuilderError::ControlBufferTooSmall);
209+
}
210+
197211
self.config.max_packet_size_0 = max_packet_size_0;
198212
Ok(self)
199213
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ pub mod endpoint;
158158
/// .product("Serial port")])
159159
/// .expect("Failed to set strings")
160160
/// .device_class(usb_serial::DEVICE_CLASS)
161-
/// .build();
161+
/// .build().unwrap();
162162
///
163163
/// // At this point the USB peripheral is enabled and a connected host will attempt to enumerate
164164
/// // it.

src/test_class.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl<B: UsbBus> TestClass<'_, B> {
9696

9797
/// Convenience method to create a UsbDevice that is configured correctly for TestClass.
9898
pub fn make_device<'a>(&self, usb_bus: &'a UsbBusAllocator<B>) -> UsbDevice<'a, B> {
99-
self.make_device_builder(usb_bus).build()
99+
self.make_device_builder(usb_bus).build().unwrap()
100100
}
101101

102102
/// Convenience method to create a UsbDeviceBuilder that is configured correctly for TestClass.

0 commit comments

Comments
 (0)