From ca366454103e28d6a85823454e64c21302627555 Mon Sep 17 00:00:00 2001 From: "Peter D. Gray" Date: Fri, 11 May 2018 11:40:04 -0400 Subject: [PATCH] stm32/usbd_hid_interface: Address possible race condition vs. interrupt. The USB IRQ may fire once USBD_HID_ClearNAK() is called and then change the last_read_len value. --- ports/stm32/usbd_hid_interface.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ports/stm32/usbd_hid_interface.c b/ports/stm32/usbd_hid_interface.c index 4ee533c21c..9ab5986b66 100644 --- a/ports/stm32/usbd_hid_interface.c +++ b/ports/stm32/usbd_hid_interface.c @@ -94,12 +94,13 @@ int usbd_hid_rx(usbd_hid_itf_t *hid, size_t len, uint8_t *buf, uint32_t timeout) } // Copy bytes from device to user buffer - memcpy(buf, hid->buffer[hid->current_read_buffer], hid->last_read_len); + int read_len = hid->last_read_len; + memcpy(buf, hid->buffer[hid->current_read_buffer], read_len); hid->current_read_buffer = !hid->current_read_buffer; // Clear NAK to indicate we are ready to read more data USBD_HID_ClearNAK(hid->usbd); // Success, return number of bytes read - return hid->last_read_len; + return read_len; }