Skip to content

Fix DeflateEncoder.GetMaxCompressedLength conservative bound - #132274

Open
BrzVlad wants to merge 1 commit into
dotnet:mainfrom
BrzVlad:fix-zlib-mobile
Open

Fix DeflateEncoder.GetMaxCompressedLength conservative bound#132274
BrzVlad wants to merge 1 commit into
dotnet:mainfrom
BrzVlad:fix-zlib-mobile

Conversation

@BrzVlad

@BrzVlad BrzVlad commented Aug 13, 2026

Copy link
Copy Markdown
Member

Previous code was using the implementation from compressBound(). This api returns a conservative bound when using the compress() APIs, which our .NET implementation doesn't use (it uses deflate directly). The compress API does a deflate with default configuration, while our API allows custom configuration (via the quality level, window size and a few others). This means that the compressBound is not valid when using the deflate API with custom parameters.

zlib exposes the deflateBound method which can be used to obtain a conservative size. The problem with this API is that it requires to know the parameters of the compression, which we don't have available in our .NET API, given GetMaxCompressedLenght is static, applying universally to all compressions. While it is not yet decided if we will add a new API to support obtaining the bound from the library, this commit adds a workaround, by computing the maximum bound for every compression configuration exposed by our .NET APIs

The formula is obtained from our zlib-ng sources in deflate.c. Mobile uses zlib however. Validated that this change works correctly on maccatalyst, ios simulator and that the current logic stands with the current open source code for zlib (which applies to android). The actual bound used by Apple and Android can always change, so we do have a small risk that our hardcoded limit will diverge. I believe the only safe solution for this would be to make use of the actual deflateBound method, which requires adjustement to our API.

Fixes #127563

Previous code was using the implementation from compressBound(). This api returns a conservative bound when using the compress() APIs, which our .NET implementation doesn't use (it uses deflate directly). The compress API does a deflate with default configuration, while our API allows custom configuration (via the quality level, window size and a few others). This means that the compressBound is not valid when using the deflate API with custom parameters.

zlib exposes the deflateBound method which can be used to obtain a conservative size. The problem with this API is that it requires to know the parameters of the compression, which we don't have available in our .NET API, given GetMaxCompressedLenght is static, applying universally to all compressions. While it is not yet decided if we will add a new API to support obtaining the bound from the library, this commit adds a workaround, by computing the maximum bound for every compression configuration exposed by our .NET APIs

The formula is obtained from our zlib-ng sources in deflate.c. Mobile uses zlib however. Validated that this change works correctly on maccatalyst, ios simulator and that the current logic stands with the current open source code for zlib (which applies to android). The actual bound used by Apple and Android can always change, so we do have a small risk that our hardcoded limit will diverge. I believe the only safe solution for this would be to make use of the actual deflateBound method, which requires adjustement to our API.
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Updates DeflateEncoder.GetMaxCompressedLength to return a more conservative, configuration-agnostic upper bound for deflate-based encoders (addressing mobile failures where GetMaxCompressedLength() under-allocated and Compress returned DestinationTooSmall), and re-enables the previously skipped window-log round-trip test.

Changes:

  • Replaces the prior compressBound()-based implementation with a managed bound derived from zlib-ng’s deflateBound() conservative paths.
  • Updates ZLibEncoder/GZipEncoder commentary to reflect the updated bound semantics and overhead assumptions.
  • Removes the mobile-only [ActiveIssue] skip for RoundTrip_AllWindowLogs now that the bound is expected to be sufficient.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateEncoder.cs Reworks GetMaxCompressedLength to a managed conservative bound intended to cover all supported compression configurations.
src/libraries/System.IO.Compression/src/System/IO/Compression/ZLibEncoder.cs Adjusts the method comment to reflect that the bound is an upper bound (not necessarily exact).
src/libraries/System.IO.Compression/src/System/IO/Compression/GZipEncoder.cs Updates comment explaining gzip overhead relative to the deflate/zlib bound.
src/libraries/Common/tests/System/IO/Compression/EncoderDecoderTestBase.cs Re-enables the all-window-log round-trip test by removing the mobile platform skip.

Comment on lines +150 to +154
// so z_stream's strstart and gzhead are always null. We also don't handle the s390 corner case.
ulong sourceLength = (ulong)inputLength;
ulong maxCompressedLength = sourceLength
+ (sourceLength == 0 ? 1u : 0u)
+ (sourceLength < 9 ? 1u : 0u)
+ ((sourceLength + 7) >> 3)
+ 3 // DEFLATE_BLOCK_OVERHEAD: (3 + 15 + 6) >> 3
+ 6; // ZLIB_WRAPLEN: zlib header (2 bytes) + Adler32 trailer (4 bytes)
const ulong wrapLength = 6; // GZIP_WRAPLEN is 18, GZipEncoder compensates for the rest
ulong maxCompressedLength = sourceLength + ((sourceLength + 7) >> 3) + ((sourceLength + 63) >> 6) + 5;
ulong storedBlockBound = sourceLength + (sourceLength >> 5) + (sourceLength >> 7) + (sourceLength >> 11) + 7;

@rzikm rzikm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM if CI passes, thanks!

@BrzVlad

BrzVlad commented Aug 13, 2026

Copy link
Copy Markdown
Member Author

/azp run runtime-extra-platforms

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compression encoder tests fail on mobile platforms with DestinationTooSmall

3 participants