Skip to content

assets: advance offset on EOF in File.Read and close file on gzip error - #1054

Open
xgbtxy wants to merge 1 commit into
prometheus:mainfrom
xgbtxy:fix/assets-read-eof-offset
Open

xgbtxy wants to merge 1 commit into
prometheus:mainfrom
xgbtxy:fix/assets-read-eof-offset

Conversation

@xgbtxy

@xgbtxy xgbtxy commented Nov 25, 2026

Copy link
Copy Markdown

Summary

Fix two contract bugs in assets/embed_gzip.go:

  1. (*File).Read does not advance f.offset on the final io.EOF chunk:
    Read checked if n == len(f.content)-f.offset { return n, io.EOF } before executing f.offset += n. As a result, once a read reached the end of the decompressed buffer, every subsequent Read call re-delivered the final chunk (or the entire file) with (n, io.EOF) instead of returning (0, io.EOF), violating Go's io.Reader contract.
  2. FileSystem.Open returns an unclosed non-nil fs.File on gzip error:
    If gzip.NewReader(f) or io.ReadAll(gr) failed, Open returned (f, err) without closing f. It now closes f and returns (nil, err).

Verification

  • Added unit tests in assets/embed_gzip_test.go (TestFileReadAfterEOF, TestOpenCorruptGzip) plus assets/testdata/corrupt.gz.
  • Before the fix, TestFileReadAfterEOF failed with n=4 / n=2 on subsequent reads after io.EOF, and TestOpenCorruptGzip failed with a non-nil *embed.openFile.
  • After the fix, go test -race -count=1 -v ./..., go vet ./..., and gofmt -l . in assets/ all pass.

Note: AI-assisted diagnosis and drafting; all code, tests, and local verification were reviewed and executed locally.

File.Read returned (n, io.EOF) before updating f.offset += n whenever a
read consumed the remaining content, causing subsequent Read calls to
re-deliver the final chunk with (n, io.EOF) instead of (0, io.EOF) in
violation of the io.Reader contract.

Also close the underlying embedded file and return (nil, err) when
FileSystem.Open fails during gzip reader initialization or decompression.

Signed-off-by: Yan Chen <yan8925298@gmail.com>
Copilot AI balanced review requested due to automatic review settings November 25, 2026 02:38

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

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