Skip to content

fix: Ensure buffer space before reading in Decompress method#127105

Open
pumpkin-bit wants to merge 6 commits intodotnet:mainfrom
pumpkin-bit:fix/zstandard-stream-buffer-truncation
Open

fix: Ensure buffer space before reading in Decompress method#127105
pumpkin-bit wants to merge 6 commits intodotnet:mainfrom
pumpkin-bit:fix/zstandard-stream-buffer-truncation

Conversation

@pumpkin-bit
Copy link
Copy Markdown

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.

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Apr 18, 2026
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @karelz, @dotnet/area-system-io-compression
See info in area-owners.md if you want to be subscribed.

Copy link
Copy Markdown
Member

@MihaZupan MihaZupan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. Can you please also add a test that covers the described case?

pumpkin-bit and others added 3 commits April 18, 2026 19:21
…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>
@pumpkin-bit
Copy link
Copy Markdown
Author

@dotnet-policy-service agree

@pumpkin-bit
Copy link
Copy Markdown
Author

Thank you. Can you please also add a test that covers the described case?

done added a test case for ZstandardStream.

@MihaZupan
Copy link
Copy Markdown
Member

Did you check that the test fails before the fix in this PR?

@pumpkin-bit
Copy link
Copy Markdown
Author

Did you check that the test fails before the fix in this PR?

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.IO.Compression community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants