Skip to content

Commit e2b6581

Browse files
stream_buffer: reject message length truncation at runtime in writer
Defect: prvWriteMessageToBuffer() can write a truncated message-length header into a message buffer when the data length does not fit the configured configMESSAGE_BUFFER_LENGTH_TYPE and configASSERT() is compiled out, desynchronizing framing at the receiver. Root cause: the writer stores the length as a configMESSAGE_BUFFER_LENGTH_TYPE value xMessageLength and only guards the "fits without truncation" condition with configASSERT( ( size_t ) xMessageLength == xDataLengthBytes ). The subsequent write was gated solely on available space, so with asserts disabled a length that overflows the length type is written truncated while the full data is copied, corrupting the message framing seen by the receiver. Fix: gate the length-plus-data write on ( ( size_t ) xMessageLength == xDataLengthBytes ) in addition to the space check. When the length cannot be represented without truncation, take the "not enough space" path and write nothing. This runtime check is the only guard against truncation when asserts are disabled. A host regression test kept outside this repository demonstrates the fault before the change and its absence afterwards (red then green).
1 parent bc05723 commit e2b6581

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

stream_buffer.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,16 +1065,21 @@ static size_t prvWriteMessageToBuffer( StreamBuffer_t * const pxStreamBuffer,
10651065
/* Ensure the data length given fits within configMESSAGE_BUFFER_LENGTH_TYPE. */
10661066
configASSERT( ( size_t ) xMessageLength == xDataLengthBytes );
10671067

1068-
if( xSpace >= xRequiredSpace )
1068+
if( ( ( size_t ) xMessageLength == xDataLengthBytes ) && ( xSpace >= xRequiredSpace ) )
10691069
{
1070-
/* There is enough space to write both the message length and the message
1070+
/* The message length fits within configMESSAGE_BUFFER_LENGTH_TYPE and
1071+
* there is enough space to write both the message length and the message
10711072
* itself into the buffer. Start by writing the length of the data, the data
10721073
* itself will be written later in this function. */
10731074
xNextHead = prvWriteBytesToBuffer( pxStreamBuffer, ( const uint8_t * ) &( xMessageLength ), sbBYTES_TO_STORE_MESSAGE_LENGTH, xNextHead );
10741075
}
10751076
else
10761077
{
1077-
/* Not enough space, so do not write data to the buffer. */
1078+
/* Either there is not enough space, or the message length cannot be
1079+
* represented by configMESSAGE_BUFFER_LENGTH_TYPE without truncation.
1080+
* Writing a truncated length header would corrupt the message
1081+
* framing at the receiver, so do not write any data to the buffer. When asserts are
1082+
* disabled this runtime check is the only guard against truncation. */
10781083
xDataLengthBytes = 0;
10791084
}
10801085
}

0 commit comments

Comments
 (0)