From 33c7d0d9b22f97ab8bce47dad1293d60825911e4 Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 23 May 2020 11:27:24 +0100 Subject: [PATCH] Fix infinite loop caused when the end of the stream is reached --- .../src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.cs index d0176afa60..74a2e6226d 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SNI/SslOverTdsStream.cs @@ -104,9 +104,16 @@ private async Task ReadInternal(byte[] buffer, int offset, int count, Cance // Account for split packets while (readBytes < TdsEnums.HEADER_LEN) { - readBytes += async ? + var readBytesForHeader = async ? await _stream.ReadAsync(packetData, readBytes, TdsEnums.HEADER_LEN - readBytes, token).ConfigureAwait(false) : _stream.Read(packetData, readBytes, TdsEnums.HEADER_LEN - readBytes); + + if (readBytesForHeader == 0) + { + throw new EndOfStreamException("End of stream reached"); + } + + readBytes += readBytesForHeader; } _packetBytes = (packetData[TdsEnums.HEADER_LEN_FIELD_OFFSET] << 8) | packetData[TdsEnums.HEADER_LEN_FIELD_OFFSET + 1];