mirror of
https://github.com/qmk/qmk_firmware.git
synced 2025-06-12 11:54:14 +00:00
Fixed possible buffer overrun in the XPLAINBridge project when in serial bridge mode.
This commit is contained in:
parent
7cef08e10e
commit
e2e1fe5aad
@ -75,6 +75,26 @@
|
|||||||
Buffer->Count = 0;
|
Buffer->Count = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Atomically determines if the specified ring buffer contains any free space. This should
|
||||||
|
* be tested before storing data to the buffer, to ensure that no data is lost due to a
|
||||||
|
* buffer overrun.
|
||||||
|
*
|
||||||
|
* \param[in,out] Buffer Pointer to a ring buffer structure to insert into
|
||||||
|
*
|
||||||
|
* \return Boolean true if the buffer contains no free space, false otherwise
|
||||||
|
*/
|
||||||
|
static inline bool RingBuffer_IsFull(RingBuff_t* const Buffer)
|
||||||
|
{
|
||||||
|
bool IsFull;
|
||||||
|
|
||||||
|
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
|
||||||
|
{
|
||||||
|
IsFull = (Buffer->Count == BUFFER_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
return IsFull;
|
||||||
|
}
|
||||||
|
|
||||||
/** Atomically inserts an element into the ring buffer.
|
/** Atomically inserts an element into the ring buffer.
|
||||||
*
|
*
|
||||||
* \param[in,out] Buffer Pointer to a ring buffer structure to insert into
|
* \param[in,out] Buffer Pointer to a ring buffer structure to insert into
|
||||||
|
@ -75,6 +75,26 @@
|
|||||||
Buffer->Count = 0;
|
Buffer->Count = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Atomically determines if the specified ring buffer contains any free space. This should
|
||||||
|
* be tested before storing data to the buffer, to ensure that no data is lost due to a
|
||||||
|
* buffer overrun.
|
||||||
|
*
|
||||||
|
* \param[in,out] Buffer Pointer to a ring buffer structure to insert into
|
||||||
|
*
|
||||||
|
* \return Boolean true if the buffer contains no free space, false otherwise
|
||||||
|
*/
|
||||||
|
static inline bool RingBuffer_IsFull(RingBuff_t* const Buffer)
|
||||||
|
{
|
||||||
|
bool IsFull;
|
||||||
|
|
||||||
|
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
|
||||||
|
{
|
||||||
|
IsFull = (Buffer->Count == BUFFER_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
return IsFull;
|
||||||
|
}
|
||||||
|
|
||||||
/** Atomically inserts an element into the ring buffer.
|
/** Atomically inserts an element into the ring buffer.
|
||||||
*
|
*
|
||||||
* \param[in,out] Buffer Pointer to a ring buffer structure to insert into
|
* \param[in,out] Buffer Pointer to a ring buffer structure to insert into
|
||||||
|
@ -84,7 +84,7 @@ int main(void)
|
|||||||
/* Read bytes from the USB OUT endpoint into the USART transmit buffer */
|
/* Read bytes from the USB OUT endpoint into the USART transmit buffer */
|
||||||
for (uint8_t DataBytesRem = CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface); DataBytesRem != 0; DataBytesRem--)
|
for (uint8_t DataBytesRem = CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface); DataBytesRem != 0; DataBytesRem--)
|
||||||
{
|
{
|
||||||
if (!(BUFFER_SIZE - USBtoUSART_Buffer.Count))
|
if (RingBuffer_IsFull(&USBtoUSART_Buffer))
|
||||||
break;
|
break;
|
||||||
|
|
||||||
RingBuffer_AtomicInsert(&USBtoUSART_Buffer, CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface));
|
RingBuffer_AtomicInsert(&USBtoUSART_Buffer, CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface));
|
||||||
|
@ -75,6 +75,26 @@
|
|||||||
Buffer->Count = 0;
|
Buffer->Count = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Atomically determines if the specified ring buffer contains any free space. This should
|
||||||
|
* be tested before storing data to the buffer, to ensure that no data is lost due to a
|
||||||
|
* buffer overrun.
|
||||||
|
*
|
||||||
|
* \param[in,out] Buffer Pointer to a ring buffer structure to insert into
|
||||||
|
*
|
||||||
|
* \return Boolean true if the buffer contains no free space, false otherwise
|
||||||
|
*/
|
||||||
|
static inline bool RingBuffer_IsFull(RingBuff_t* const Buffer)
|
||||||
|
{
|
||||||
|
bool IsFull;
|
||||||
|
|
||||||
|
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
|
||||||
|
{
|
||||||
|
IsFull = (Buffer->Count == BUFFER_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
return IsFull;
|
||||||
|
}
|
||||||
|
|
||||||
/** Atomically inserts an element into the ring buffer.
|
/** Atomically inserts an element into the ring buffer.
|
||||||
*
|
*
|
||||||
* \param[in,out] Buffer Pointer to a ring buffer structure to insert into
|
* \param[in,out] Buffer Pointer to a ring buffer structure to insert into
|
||||||
|
@ -120,7 +120,7 @@ void UARTBridge_Task(void)
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
/* Read bytes from the USB OUT endpoint into the UART transmit buffer */
|
/* Read bytes from the USB OUT endpoint into the UART transmit buffer */
|
||||||
if (CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface))
|
if (CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface) && !(RingBuffer_IsFull(&USBtoUART_Buffer)))
|
||||||
RingBuffer_AtomicInsert(&USBtoUART_Buffer, CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface));
|
RingBuffer_AtomicInsert(&USBtoUART_Buffer, CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface));
|
||||||
|
|
||||||
/* Check if the software UART flush timer has expired */
|
/* Check if the software UART flush timer has expired */
|
||||||
|
Loading…
Reference in New Issue
Block a user