Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move buffer expansion outside of the loop to prevent unbounded grow
  • Loading branch information
rzikm authored and github-actions committed Mar 17, 2023
commit 9ac03e74ce7e3dd90690f0341cde1e43b83875d7
Original file line number Diff line number Diff line change
Expand Up @@ -684,10 +684,15 @@ private async ValueTask<int> EnsureFullTlsFrameAsync<TIOAdapter>(CancellationTok
return frameSize;
}

// make sure we have space to read into, there are two cases which can happen
// - we know the exact frame size (frameSize != int.MaxValue) - we make sure we have space for the whole frame
// - we don't know the frame size (frameSize == int.MaxValue) - we move existing data to the beginning of the buffer (they will be couple of bytes only)
_buffer.EnsureAvailableSpace(Math.Min(frameSize, _buffer.Capacity) - _buffer.EncryptedLength);

while (_buffer.EncryptedLength < frameSize)
{
// make sure we have space to read into
_buffer.EnsureAvailableSpace(Math.Min(frameSize, _buffer.Capacity) - _buffer.EncryptedLength);
// there should be space left to read into
Debug.Assert(_buffer.AvailableLength > 0, "_buffer.AvailableBytes > 0");

// We either don't have full frame or we don't have enough data to even determine the size.
int bytesRead = await TIOAdapter.ReadAsync(InnerStream, _buffer.AvailableMemory, cancellationToken).ConfigureAwait(false);
Expand Down