Skip to content

fix: reset read offset when Read drains the current block - #76

Merged
klauspost merged 1 commit into
klauspost:masterfrom
stefanfrede:fix-writeto-stale-read-offset
Oct 9, 2026
Merged

klauspost merged 1 commit into
klauspost:masterfrom
stefanfrede:fix-writeto-stale-read-offset

Conversation

@stefanfrede

@stefanfrede stefanfrede commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #74

Reader.Read set z.current = nil after draining a block without resetting z.roff, so a following WriteTo (io.Copy) sliced the nil z.current with a stale offset and panicked. Resetting the offset together with z.current fixes it.

Tests: two regression tests that panic without the change (one through archive/tar, which ends exactly at the end-of-archive marker). Checked with gofmt, go vet, go test -cpu=1,2,4 . and go test -cpu=2 -race -short .

Summary by CodeRabbit

  • Bug Fixes
    • Fixed copying from a gzip reader after its contents have been consumed. Copying now correctly returns no additional data without reporting an error, including when the reader has reached the end of an archived stream. This improves reliability when callers attempt to copy from a reader after reading its contents.

Read set z.current to nil after draining a block but left z.roff at the offset of earlier partial reads. A following WriteTo then sliced the nil z.current with that stale offset and panicked (slice bounds out of range [N:0]). This is hit by archive/tar reading a stream that ends exactly at the end-of-archive marker, followed by io.Copy.

Adds regression tests for both cases.

Fixes klauspost#74
@coderabbitai

coderabbitai Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 62d993f0-9d65-40a2-9189-b0fcc1428e24

📥 Commits

Reviewing files that changed from the base of the PR and between 740cd9e and 34420f0.


📒 Files selected for processing (2)
  • gunzip.go
  • writeto_stale_offset_test.go

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.



📝 Walkthrough

Walkthrough

Reader.Read now resets its read offset after releasing the current block. Two regression tests check that copying from the reader after consuming its data completes without error.

Changes

Reader offset reset

Layer / File(s) Summary
Reset offset and test drained reads
gunzip.go, writeto_stale_offset_test.go
Reader.Read resets roff after releasing the current block. Tests cover copying after draining a block and after consuming a tar stream.

Priority: ➖ Normal

Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to 34420

This fixes a panic when io.Copy or WriteTo follows a Read that drains the current block. The change is a single localized offset reset with regression tests, so merge risk is minimal.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: resetting the read offset when Read drains the current block. This matches the implementation and regression tests.
Linked Issues check Passed Issue #74 is open and directly linked. Reader.Read now sets z.roff = 0 when it clears z.current, so a later WriteTo cannot use a stale offset to slice the nil block. `writeto_stale_offset_test…
Out of Scope Changes check Passed The reviewed changes contain one state-reset fix in gunzip.go and two regression tests in writeto_stale_offset_test.go. Both changes directly address issue #74. No unrelated production or test cha…
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 2 files.


✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR


  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@klauspost
klauspost merged commit e1b5b31 into klauspost:master Oct 9, 2026
13 checks passed
AdamMagued added a commit to AdamMagued/pgzip that referenced this pull request Oct 12, 2026
klauspost pushed a commit that referenced this pull request Oct 14, 2026
* feat: resolve issue #24 - Parallel zlib writer implementation

Signed-off-by: AdamMagued <[email protected]>

* fix(zlib): address concurrency and error handling review findings in reader and writer

* zlib: merge master, port #76, wait for output listener on Reset, and format

---------

Signed-off-by: AdamMagued <[email protected]>
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.

Reader.WriteTo panics (slice bounds out of range [N:0]) after Read has drained the current block

2 participants