fix: Ensure buffer space before reading in Decompress method#127105
Open
pumpkin-bit wants to merge 6 commits intodotnet:mainfrom
Open
fix: Ensure buffer space before reading in Decompress method#127105pumpkin-bit wants to merge 6 commits intodotnet:mainfrom
pumpkin-bit wants to merge 6 commits intodotnet:mainfrom
Conversation
Contributor
|
Tagging subscribers to this area: @karelz, @dotnet/area-system-io-compression |
This was referenced Apr 18, 2026
MihaZupan
reviewed
Apr 18, 2026
Member
MihaZupan
left a comment
There was a problem hiding this comment.
Thank you. Can you please also add a test that covers the described case?
…Zstandard/ZstandardStream.Decompress.cs Co-authored-by: Miha Zupan <mihazupan.zupan1@gmail.com>
…Zstandard/ZstandardStream.Decompress.cs Co-authored-by: Miha Zupan <mihazupan.zupan1@gmail.com>
Author
|
@dotnet-policy-service agree |
Author
done added a test case for ZstandardStream. |
Member
|
Did you check that the test fails before the fix in this PR? |
Author
Yes, I checked using a test to simulate the situation with and without my fix. Without the fix, the decompressor caught an exception. With my fix, the buffer moved correctly without errors. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The essence of the bug:
During decompression, a portion of compressed data ends exactly at the boundary of a 64 kb buffer, and the remaining bytes, not yet decompressed by the decoder, are waiting for the next portion, resulting in a exception. The buffer is physically full, and AvailableSpan returns an empty fragment,
Span<byte>empty reading from the file into an empty Span returns 0 bytes read.Solution:
Checks have been added to the main Read and ReadAsync loops. If AvailableLength is zero, we force a call to
_buffer.EnsureAvailableSpace(1);.the bytes are shifted to the beginning of the array, the read is released, and the stream then reads new data.