From 2cbde409e0cbf2f778a8cfce0c3bd35ce1ad86b5 Mon Sep 17 00:00:00 2001 From: Ivan Zlatanov Date: Mon, 13 Dec 2021 14:41:42 +0200 Subject: [PATCH] Fixing a possible null reference error in WebSocket deflate. (#62428) Fixes #62422 --- .../Net/WebSockets/Compression/WebSocketDeflater.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Net.WebSockets/src/System/Net/WebSockets/Compression/WebSocketDeflater.cs b/src/libraries/System.Net.WebSockets/src/System/Net/WebSockets/Compression/WebSocketDeflater.cs index 7d8eb832c2e..7c8db8eedcc 100644 --- a/src/libraries/System.Net.WebSockets/src/System/Net/WebSockets/Compression/WebSocketDeflater.cs +++ b/src/libraries/System.Net.WebSockets/src/System/Net/WebSockets/Compression/WebSocketDeflater.cs @@ -144,7 +144,13 @@ private unsafe void UnsafeDeflate(ReadOnlySpan input, Span output, o consumed = input.Length - (int)_stream.AvailIn; written = output.Length - (int)_stream.AvailOut; - needsMoreBuffer = errorCode == ErrorCode.BufError || _stream.AvailIn > 0; + // It is important here to also check that we haven't + // exhausted the output buffer because after deflating we're + // always going to issue a flush and a flush with empty output + // is going to throw. + needsMoreBuffer = errorCode == ErrorCode.BufError + || _stream.AvailIn > 0 + || written == output.Length; } } @@ -152,6 +158,7 @@ private unsafe int UnsafeFlush(Span output, out bool needsMoreBuffer) { Debug.Assert(_stream is not null); Debug.Assert(_stream.AvailIn == 0); + Debug.Assert(output.Length > 0); fixed (byte* fixedOutput = output) { -- GitLab