Skip to content

Commit 3142927

Browse files
authored
Merge pull request hathach#2852 from GuavTek/recover_zero_length_desc
USBH: Recover from unexpected descriptor size
2 parents 5bb90ef + 3cacb01 commit 3142927

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/host/usbh.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,6 +1654,13 @@ static bool _parse_configuration_descriptor(uint8_t dev_addr, tusb_desc_configur
16541654

16551655
// parse each interfaces
16561656
while( p_desc < desc_end ) {
1657+
if ( 0 == tu_desc_len(p_desc) ) {
1658+
// A zero length descriptor indicates that the device is off spec (e.g. wrong wTotalLength).
1659+
// Parsed interfaces should still be usable
1660+
TU_LOG_USBH("Encountered a zero-length descriptor after %u bytes\r\n", (uint32_t)p_desc - (uint32_t)desc_cfg);
1661+
break;
1662+
}
1663+
16571664
uint8_t assoc_itf_count = 1;
16581665

16591666
// Class will always starts with Interface Association (if any) and then Interface descriptor

src/tusb.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,10 @@ uint16_t tu_desc_get_interface_total_len(tusb_desc_interface_t const* desc_itf,
258258
p_desc = tu_desc_next(p_desc);
259259

260260
while (len < max_len) {
261+
if (tu_desc_len(p_desc) == 0) {
262+
// Escape infinite loop
263+
break;
264+
}
261265
// return on IAD regardless of itf count
262266
if (tu_desc_type(p_desc) == TUSB_DESC_INTERFACE_ASSOCIATION) {
263267
return len;

0 commit comments

Comments
 (0)