Fix infinite loop in writing gzip header/footer#547
Merged
Byron merged 3 commits intoMay 3, 2026
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes a potential infinite loop in flate2::write::GzEncoder when the wrapped Write impl returns Ok(0) while writing gzip header/footer bytes, aligning gzip-specific direct writes with zio::Writer::dump behavior (convert to WriteZero).
Changes:
- Add
write_nonzerohelper to convertOk(0)on non-empty buffers intoio::ErrorKind::WriteZerofor gzip header/footer direct writes. - Update gzip header write loop and final 8-byte footer write loop to use the new helper.
- Add regression tests intended to cover both “header zero-write” and “footer zero-write” cases.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/gz/write.rs |
Prevents infinite retry loops by treating zero-progress writes during gzip header/footer emission as WriteZero. |
tests/zero-write.rs |
Adds integration tests for zero-progress writes during gzip header and footer finalization. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Byron
requested changes
May 3, 2026
Byron
left a comment
Member
There was a problem hiding this comment.
Thanks a lot for the fix!
This looks good to me, just a few comments I'd like to add to make it clear where some magic numbers are coming from.
Member
Author
|
Thanks for the quick review! Both your and copilot comments should now be addressed. |
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.
Summary
Fix
write::GzEncoderspinning forever when the wrapped writer returnsOk(0)while writing gzip header or footer bytes.Problem
Most write paths in flate2 go through
zio::Writer::dump, which treatsOk(0)for a non-empty buffer asio::ErrorKind::WriteZero.write::GzEncoderhas two gzip-specific direct write loops:Those loops previously retried after
Ok(0)without making progress. A writer that repeatedly returnsOk(0)for a non-empty buffer could therefore causeGzEncoder::try_finish/finish/drop finalization to spin indefinitely.Fix
Add a small helper for gzip’s direct writes that mirrors the shared writer behavior:
Ok(0)on a non-empty buffer is converted toWriteZero.Tests
Added regression coverage for both spin sites:
Validation:
cargo test --test zero-writecargo test gz::testscargo testCredits
The issue was discovered by GPT-5.5 xhigh