Skip to content

Commit

Permalink
Fix infinite loop caused when the end of the stream is reached (#577)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdougie committed Jun 5, 2020
1 parent cd38e39 commit c2eb0c0
Showing 1 changed file with 8 additions and 1 deletion.
Expand Up @@ -104,9 +104,16 @@ private async Task<int> 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];
Expand Down

0 comments on commit c2eb0c0

Please sign in to comment.