From 1e4aab6d4004b79a7061a7402cb97e9a7ed7c640 Mon Sep 17 00:00:00 2001 From: Miles Frain Date: Mon, 22 Feb 2021 21:27:26 -0800 Subject: [PATCH] Apply SpacesAvailable() fix Original author: RichardBarry --- stream_buffer.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/stream_buffer.c b/stream_buffer.c index 6673ef1ea..462f7b677 100644 --- a/stream_buffer.c +++ b/stream_buffer.c @@ -280,7 +280,7 @@ static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer, { pucAllocatedMemory = NULL; } - + if( pucAllocatedMemory != NULL ) { @@ -497,11 +497,19 @@ size_t xStreamBufferSpacesAvailable( StreamBufferHandle_t xStreamBuffer ) { const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer; size_t xSpace; + volatile size_t xOriginalTail; configASSERT( pxStreamBuffer ); - xSpace = pxStreamBuffer->xLength + pxStreamBuffer->xTail; - xSpace -= pxStreamBuffer->xHead; + /* The code below reads xTail and then xHead. This is safe if the stream + buffer is updated once between the two reads - but not if the stream buffer + is updated more than once between the two reads - hence the loop. */ + do + { + xOriginalTail = pxStreamBuffer->xTail; + xSpace = pxStreamBuffer->xLength + pxStreamBuffer->xTail; + xSpace -= pxStreamBuffer->xHead; + } while( xOriginalTail != pxStreamBuffer->xTail ); xSpace -= ( size_t ) 1; if( xSpace >= pxStreamBuffer->xLength )