Skip to content

Commit 2689c22

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 741185f commit 2689c22

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

stream_buffer.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,17 +728,24 @@ 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;
731732

732733
if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
733734
{
734735
/* This is a message buffer, as opposed to a stream buffer. */
735736

737+
/* Convert xDataLengthBytes to the message length type. */
738+
xBufferLengthData = ( configMESSAGE_BUFFER_LENGTH_TYPE ) xDataLengthBytes;
739+
740+
/* Ensure the data length given fits within configMESSAGE_BUFFER_LENGTH_TYPE. */
741+
configASSERT( ( size_t ) xBufferLengthData == xDataLengthBytes );
742+
736743
if( xSpace >= xRequiredSpace )
737744
{
738745
/* There is enough space to write both the message length and the message
739746
* itself into the buffer. Start by writing the length of the data, the data
740747
* itself will be written later in this function. */
741-
xNextHead = prvWriteBytesToBuffer( pxStreamBuffer, ( const uint8_t * ) &( xDataLengthBytes ), sbBYTES_TO_STORE_MESSAGE_LENGTH, xNextHead );
748+
xNextHead = prvWriteBytesToBuffer( pxStreamBuffer, ( const uint8_t * ) &( xBufferLengthData ), sbBYTES_TO_STORE_MESSAGE_LENGTH, xNextHead );
742749
}
743750
else
744751
{

0 commit comments

Comments
 (0)