dwc2/hcd: guard against zero ep_size to prevent divide-by-zero during enumeration#3526
Closed
Copilot wants to merge 3 commits into
Closed
dwc2/hcd: guard against zero ep_size to prevent divide-by-zero during enumeration#3526Copilot wants to merge 3 commits into
Copilot wants to merge 3 commits into
Conversation
Co-authored-by: hathach <249515+hathach@users.noreply.github.com>
Co-authored-by: hathach <249515+hathach@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix divide-by-zero error in DWC2 host controller
dwc2/hcd: guard against zero ep_size to prevent divide-by-zero during enumeration
Mar 5, 2026
TinyUSB Average Code Size Metrics
Input files
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A device reporting
bMaxPacketSize0 = 0(malformed descriptor) causes an integer divide-by-zero incal_packet_count()viatu_div_ceil(len, 0)when the DWC2 HCD attempts to compute packet count for a control transfer.Changes
hcd_edpt_open()— extractep_sizefromtu_edpt_packet_size()andTU_ASSERT(ep_size != 0)before writing to the hardware channel register; this fails the endpoint open gracefully (returnsfalse) rather than allowing a zero value to propagate into transfer operationscal_packet_count()— extend existinglen == 0early-return to also coverep_size == 0as defense-in-depthOriginal prompt
This section details on the original issue you should resolve
<issue_title>DWC2 host divide-by-zero when connecting HID device (esp32-arduino)</issue_title>
<issue_description>### Operating System
MacOS
Commit SHA
TinyUSB 0.20.1 (as in esp32-arduino-lib-builder); note to confirm on upstream.
Board
ESP32S3-USB-OTG (host mode)
Firmware
tuh_descriptor_get_device(),tuh_task_ext(), etc., and DWC2 as the host controller.Reproducible with any firmware that uses the DWC2 host driver and performs control transfers (e.g. get device descriptor) when a device is connected.
What happened ?
Integer divide-by-zero in the DWC2 host controller driver during USB host enumeration when a HID device (e.g. USB mouse) is connected.
cal_packet_count()insrc/portable/synopsys/dwc2/hcd_dwc2.c(around line 299), which callstu_div_ceil(len, ep_size).ep_sizecan be 0 when the default control endpoint (EP0) is used with an invalid or unset max packet size (e.g.bMaxPacketSize0from the first 8 bytes of the device descriptor is 0 or not yet valid). Dividing byep_sizethen triggers the crash.tuh_descriptor_get_device→hcd_setup_send→hcd_edpt_xfer→edpt_xfer_kickoff→channel_xfer_start→cal_packet_count(len, ep_size)→tu_div_ceil(len, 0)→ IntegerDivideByZero.So the bug is: no guard against
ep_size == 0in the DWC2 HCD when computing packet count.How to reproduce ?
tuh_descriptor_get_device()and related APIs).tu_div_ceil(called fromcal_packet_countinhcd_dwc2.c).Exact steps on our setup:
esp32s3.idf.py -p <port> flash monitor.Sketch: