Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

USB buffer overflow on clear stall operation #9

Open
pierre-x opened this issue Jan 20, 2025 · 6 comments
Open

USB buffer overflow on clear stall operation #9

pierre-x opened this issue Jan 20, 2025 · 6 comments
Assignees
Labels
bug Something isn't working hal HAL-LL driver-related issue or pull-request. internal bug tracker Issue confirmed and logged into the internal bug tracking system pcd USB Peripheral Control Driver usb Universal Serial Bus

Comments

@pierre-x
Copy link

pierre-x commented Jan 20, 2025

Inside stm32f0xx_hal_pcd.c, on normal operation, when the receive buffer is not full, the receive callback CDC_Receive_FS calls USBD_CDC_ReceivePacket, which re-arms the endpoint to receive new data and sets ep->xfer_count to zero.

In the case of a full buffer, the host may reach a timeout and request a Clear Stall. USBD_LL_ClearStallEP will call HAL_PCD_EP_ClrStall to re-arm the endpoint WITHOUT resetting ep->xfer_count, which will cause a buffer overflow when receiving the next packet.

IMHO, HAL_PCD_EP_ClrStall() should include an ep->xfer_count = 0. The tricky part: the host can send Clear Stalls at any time, even when the buffer isn't full and the device isn't Stalled, in this case the buffer index shouldn't be cleared.

So maybe the solution is something like that:

HAL_StatusTypeDef HAL_PCD_EP_ClrStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
{
    // [...]
    if(ep->is_flowcontrol_triggered)
    {
        ep->xfer_count = 0;
    }
@ALABSTM ALABSTM self-assigned this Jan 24, 2025
@ALABSTM ALABSTM added bug Something isn't working hal HAL-LL driver-related issue or pull-request. usb Universal Serial Bus pcd USB Peripheral Control Driver labels Jan 24, 2025
@ALABSTM
Copy link
Contributor

ALABSTM commented Jan 30, 2025

Hi @pierre-x,

Thank you for this analysis and the proposal you made. I will forward your report to our development teams for further analysis. I will keep you informed.

With regards,

@ALABSTM ALABSTM moved this from To do to Analyzed in stm32cube-mcu-hal-dashboard Jan 30, 2025
@ALABSTM
Copy link
Contributor

ALABSTM commented Jan 30, 2025

@pierre-x,

May I ask whether the use case you reported has been tested by any means and the described issue reproduced, or is it analysis based on code review?

Thank you,

@ALABSTM ALABSTM added the internal bug tracker Issue confirmed and logged into the internal bug tracking system label Jan 30, 2025
@ALABSTM
Copy link
Contributor

ALABSTM commented Jan 30, 2025

ST Internal Reference: 201910

@pierre-x
Copy link
Author

May I ask whether the use case you reported has been tested by any means and the described issue reproduced, or is it analysis based on code review?

It's a bug we have encountered, and that we can reproduce 100% of the time. We have corrected it as described in the bug report.

I can share by email some part of our source code if necessary.

@ALABSTM
Copy link
Contributor

ALABSTM commented Feb 7, 2025

Hi @pierre-x,

May I ask you whether you can share a sample project allowing to reproduce the issue?

Thank you,

@ALABSTM
Copy link
Contributor

ALABSTM commented Feb 13, 2025

Hi @pierre-x,

Any sample code or project to share please?

Thank you,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working hal HAL-LL driver-related issue or pull-request. internal bug tracker Issue confirmed and logged into the internal bug tracking system pcd USB Peripheral Control Driver usb Universal Serial Bus
Projects
Development

No branches or pull requests

2 participants