Fix Deflate/Brotli/CryptoStream handling of partial and zero-byte reads (#53644)
authorStephen Toub <stoub@microsoft.com>
Fri, 11 Jun 2021 21:31:15 +0000 (17:31 -0400)
committerGitHub <noreply@github.com>
Fri, 11 Jun 2021 21:31:15 +0000 (17:31 -0400)
commit68dec6ac17053b1d2c7bfead7e785c18ccdc8dd0
tree7e5dd286d77e22df357b0a74589cc0b50e7a469d
parentffcef4afbcb2f4cfc88f0c063a12b1e9b84d36ba
Fix Deflate/Brotli/CryptoStream handling of partial and zero-byte reads (#53644)

Stream.Read{Async} is supposed to return once at least a byte of data is available, and in particular, if there's any data already available, it shouldn't block.  But Read{Async} on DeflateStream (and thus also GZipStream and ZLibStream), BrotliStream, and CryptoStream won't return until either it hits the end of the stream or the caller's buffer is filled.  This makes it behave very unexpectedly when used in a context where the app is using a large read buffer but expects to be able to process data as it's available, e.g. in networked streaming scenarios where messages are being sent as part of bidirectional communication.

This fixes that by stopping looping once any data is consumed.  Just doing that, though, caused problems for zero-byte reads.  Zero-byte reads are typically used by code that's trying to delay-allocate a buffer for the read data until data will be available to read.  At present, however, zero-byte reads return immediately regardless of whether data is available to be consumed.  I've changed the flow to make it so that zero-byte reads don't return until there's at least some data available as input to the inflater/transform (this, though, doesn't 100% guarantee the inflater/transform will be able to produce output data).

Note that both of these changes have the potential to introduce breaks into an app that erroneously depended on these implementation details:
- If an app passing in a buffer of size N to Read{Async} depended on that call always producing the requested number of bytes (rather than what the Stream contract defines), they might experience behavioral changes.
- If an app passing in a zero-byte buffer expected it to return immediately, it might instead end up waiting until data was actually available.
src/libraries/Common/tests/StreamConformanceTests/System/IO/StreamConformanceTests.cs
src/libraries/Common/tests/System/IO/Compression/CompressionStreamTestBase.cs
src/libraries/Common/tests/System/IO/Compression/ZipTestHelper.cs
src/libraries/System.IO.Compression.Brotli/src/System/IO/Compression/BrotliStream.cs
src/libraries/System.IO.Compression.Brotli/src/System/IO/Compression/dec/BrotliStream.Decompress.cs
src/libraries/System.IO.Compression.Brotli/src/System/IO/Compression/enc/BrotliStream.Compress.cs
src/libraries/System.IO.Compression.Brotli/tests/CompressionStreamUnitTests.Brotli.cs
src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateZLib/DeflateStream.cs
src/libraries/System.Security.Cryptography.Encoding/tests/Base64TransformsTests.cs
src/libraries/System.Security.Cryptography.Primitives/src/System/Security/Cryptography/CryptoStream.cs
src/libraries/System.Security.Cryptography.Primitives/tests/CryptoStream.cs