From 2d629ffa520ce46fa7877a0f1fb9833029ea63a1 Mon Sep 17 00:00:00 2001 From: Phong Nguyen Date: Thu, 5 Feb 2026 10:44:53 +0700 Subject: [PATCH 1/3] Fix issue overflow when reading data --- src/USBHostSerial.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/USBHostSerial.cpp b/src/USBHostSerial.cpp index 3de4697..0959841 100644 --- a/src/USBHostSerial.cpp +++ b/src/USBHostSerial.cpp @@ -118,8 +118,8 @@ uint8_t USBHostSerial::read() { std::size_t USBHostSerial::read(uint8_t *dest, std::size_t size) { std::size_t retVal = 0; std::size_t pxItemSize = 0; - while (size > pxItemSize) { - void *ret = xRingbufferReceiveUpTo(_rx_buf_handle, &pxItemSize, pdMS_TO_TICKS(1), size - pxItemSize); + while (size > retVal) { + void *ret = xRingbufferReceiveUpTo(_rx_buf_handle, &pxItemSize, pdMS_TO_TICKS(1), size - retVal); if (ret) { std::memcpy(dest + retVal, ret, pxItemSize); retVal += pxItemSize; From 59720d5d723a82f96a28317c756e0cce1e9df2d2 Mon Sep 17 00:00:00 2001 From: Phong Nguyen <118428350+urbytes21@users.noreply.github.com> Date: Thu, 5 Feb 2026 23:01:49 +0700 Subject: [PATCH 2/3] Explicitly stop read when remaining size is zero --- src/USBHostSerial.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/USBHostSerial.cpp b/src/USBHostSerial.cpp index 0959841..37f3eba 100644 --- a/src/USBHostSerial.cpp +++ b/src/USBHostSerial.cpp @@ -119,14 +119,20 @@ std::size_t USBHostSerial::read(uint8_t *dest, std::size_t size) { std::size_t retVal = 0; std::size_t pxItemSize = 0; while (size > retVal) { + std::size_t remaining = size - retVal; + if(remaining == 0){ + break; + } + void *ret = xRingbufferReceiveUpTo(_rx_buf_handle, &pxItemSize, pdMS_TO_TICKS(1), size - retVal); - if (ret) { + + if(!ret || pxItemSize == 0){ + break; + } + std::memcpy(dest + retVal, ret, pxItemSize); retVal += pxItemSize; vRingbufferReturnItem(_rx_buf_handle, ret); - } else { - break; - } } return retVal; } From 68e1a687f41c267761fa89fcac3d551d7cb53265 Mon Sep 17 00:00:00 2001 From: Phong Nguyen <118428350+urbytes21@users.noreply.github.com> Date: Thu, 5 Feb 2026 23:01:49 +0700 Subject: [PATCH 3/3] Explicitly stop read when remaining size is zero --- src/USBHostSerial.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/USBHostSerial.cpp b/src/USBHostSerial.cpp index 0959841..e04ea2f 100644 --- a/src/USBHostSerial.cpp +++ b/src/USBHostSerial.cpp @@ -119,14 +119,20 @@ std::size_t USBHostSerial::read(uint8_t *dest, std::size_t size) { std::size_t retVal = 0; std::size_t pxItemSize = 0; while (size > retVal) { - void *ret = xRingbufferReceiveUpTo(_rx_buf_handle, &pxItemSize, pdMS_TO_TICKS(1), size - retVal); - if (ret) { - std::memcpy(dest + retVal, ret, pxItemSize); - retVal += pxItemSize; - vRingbufferReturnItem(_rx_buf_handle, ret); - } else { + std::size_t remaining = size - retVal; + if(remaining == 0){ break; } + + void *ret = xRingbufferReceiveUpTo(_rx_buf_handle, &pxItemSize, pdMS_TO_TICKS(1), remaining); + + if(!ret || pxItemSize == 0){ + break; + } + + std::memcpy(dest + retVal, ret, pxItemSize); + retVal += pxItemSize; + vRingbufferReturnItem(_rx_buf_handle, ret); } return retVal; }