Skip to content

Commit fd05bb5

Browse files
committed
Fix prvWriteMessageToBuffer on big endian
prvWriteMessageToBuffer wrote the first sbBYTES_TO_STORE_MESSAGE_LENGTH bytes of the size_t-typed length to the buffer as the data length. While this functions on little endian, it copies the wrong bytes on big endian. This fix converts the length to configMESSAGE_BUFFER_LENGTH_TYPE first, and then copies the exact amount, thus fixing the issue. Additionally it adds an assert to verify the size is not greater than the max value of configMESSAGE_BUFFER_LENGTH_TYPE; previously this would truncate silently.
1 parent 384ffc5 commit fd05bb5

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

stream_buffer.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,10 @@ static size_t prvWriteMessageToBuffer( StreamBuffer_t * const pxStreamBuffer,
728728
size_t xRequiredSpace )
729729
{
730730
size_t xNextHead = pxStreamBuffer->xHead;
731+
configMESSAGE_BUFFER_LENGTH_TYPE xBufferLengthData = ( configMESSAGE_BUFFER_LENGTH_TYPE ) xDataLengthBytes;
732+
733+
/* Ensure the data length given fits within configMESSAGE_BUFFER_LENGTH_TYPE. */
734+
configASSERT( ( size_t ) xBufferLengthData == xDataLengthBytes );
731735

732736
if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
733737
{
@@ -738,7 +742,7 @@ static size_t prvWriteMessageToBuffer( StreamBuffer_t * const pxStreamBuffer,
738742
/* There is enough space to write both the message length and the message
739743
* itself into the buffer. Start by writing the length of the data, the data
740744
* itself will be written later in this function. */
741-
xNextHead = prvWriteBytesToBuffer( pxStreamBuffer, ( const uint8_t * ) &( xDataLengthBytes ), sbBYTES_TO_STORE_MESSAGE_LENGTH, xNextHead );
745+
xNextHead = prvWriteBytesToBuffer( pxStreamBuffer, ( const uint8_t * ) &( xBufferLengthData ), sbBYTES_TO_STORE_MESSAGE_LENGTH, xNextHead );
742746
}
743747
else
744748
{

0 commit comments

Comments
 (0)