Size compression bounds for incompressible input past 280 KB - #44
Merged
Merged
Conversation
The Deflate, Gzip, ZLib and Brotli providers allowed one 5-byte block header per 64 KB. zlib closes a block about every 16 KB, so random, encrypted or already-compressed input above roughly 280 KB outgrew the bound: TryCompress into a buffer of that size returned false and Compress threw InvalidOperationException. The deflate family now uses zlib's deflateBound for default parameters plus each container's header and trailer, computed in long so a large input throws instead of wrapping. Brotli delegates to BrotliEncoder.GetMaxCompressedLength. The contract test now covers 1 MiB and 10 MiB of random data. Fixes #39 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NHprKRwyYopkJnUyhF3KSr
The three providers carried identical copies of the deflateBound calculation, which SonarCloud's duplication gate rejected. It now lives in Shared/DeflateBound.cs, linked into each project the way HmacKeyedHashCore is, and each provider passes its container overhead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NHprKRwyYopkJnUyhF3KSr
SonarCloud's copy-paste detection ignores literal values, so the three one-line GetMaxCompressedLength bodies, which differed only in the overhead number, still read as a duplicated block with the surrounding provider code. Each provider now passes a named DeflateBound constant, which also documents where 0, 6 and 18 come from. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NHprKRwyYopkJnUyhF3KSr
|
This was referenced Sep 28, 2026
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.



Fixes #39
What changed
GetMaxCompressedLengthassumed one 5-byte stored-block header per 64 KB. zlib closes a block each time its literal buffer fills, which is roughly every 16 KB. So for incompressible input above about 280 KB, the output grew past the bound.TryCompressthen returnedfalseinto a buffer of that size, andCompress(byte[])threwInvalidOperationException.deflateBoundfor the default parameters:n + (n>>12) + (n>>14) + (n>>25) + 13, plus the container overhead (+18 for gzip, +6 for zlib). The bound is computed inlong. Aboveint.MaxValueit now throwsArgumentOutOfRangeExceptioninstead of wrapping to a small or negative value.BrotliEncoder.GetMaxCompressedLength. That method also throwsArgumentOutOfRangeExceptionwhen the length is negative or too large to bound. This is a small behaviour change: before, a nonsensical length returned a nonsensical value.Measured on net10.0 (zlib-ng), raw deflate at
Optimalwith random input:The headroom is small, as expected:
deflateBoundis zlib's documented worst case for these parameters, not an estimate. If you want a wider safety margin for zlib builds other than the one .NET bundles, adding a constant is cheap. I kept to the documented bound.Tests
Compression_Bound_Holds_For_Incompressible_Inputnow runs at 4 KB, 1 MiB and 10 MiB of random data for every compression provider.Compress_Succeeds_For_Large_Incompressible_Inputcovers theCompress(byte[])path that used to throw.Compression_Bound_Does_Not_Overflowchecks that a length nearint.MaxValueeither throws or returns a positive bound.🤖 Generated with Claude Code
https://claude.ai/code/session_01NHprKRwyYopkJnUyhF3KSr
Generated by Claude Code