Skip to content

Fix infinite loop in writing gzip header/footer#547

Merged
Byron merged 3 commits into
rust-lang:mainfrom
Shnatsel:fix-infinite-loop-in-gzip-header-write
May 3, 2026
Merged

Fix infinite loop in writing gzip header/footer#547
Byron merged 3 commits into
rust-lang:mainfrom
Shnatsel:fix-infinite-loop-in-gzip-header-write

Conversation

@Shnatsel

@Shnatsel Shnatsel commented May 2, 2026

Copy link
Copy Markdown
Member

Summary

Fix write::GzEncoder spinning forever when the wrapped writer returns Ok(0) while writing gzip header or footer bytes.

Problem

Most write paths in flate2 go through zio::Writer::dump, which treats Ok(0) for a non-empty buffer as io::ErrorKind::WriteZero.

write::GzEncoder has two gzip-specific direct write loops:

  • writing the gzip header
  • writing the final 8-byte gzip footer

Those loops previously retried after Ok(0) without making progress. A writer that repeatedly returns Ok(0) for a non-empty buffer could therefore cause GzEncoder::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 to WriteZero.

Tests

Added regression coverage for both spin sites:

  • zero-length progress while writing the gzip header
  • zero-length progress while writing the gzip footer

Validation:

  • cargo test --test zero-write
  • cargo test gz::tests
  • cargo test

Credits

The issue was discovered by GPT-5.5 xhigh

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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_nonzero helper to convert Ok(0) on non-empty buffers into io::ErrorKind::WriteZero for 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.

Comment thread tests/zero-write.rs Outdated

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

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.

Comment thread tests/zero-write.rs
Comment thread tests/zero-write.rs
@Shnatsel

Shnatsel commented May 3, 2026

Copy link
Copy Markdown
Member Author

Thanks for the quick review! Both your and copilot comments should now be addressed.

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

Great, let's merge this.

@Byron
Byron merged commit aa62dd4 into rust-lang:main May 3, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants