fix(internal/utils): keep byteReader position after failed seeks - #1005
Conversation
zeroshade
left a comment
There was a problem hiding this comment.
Verified both fixes against current main:
-
Seekunconditionally didr.pos = int(pos). Sincebytes.Reader.Seekreturnspos = 0on failure (invalid whence or negative resulting position), a failed seek corrupted the tracked position to 0 while the underlying reader stayed put — leavingPeekandReaddisagreeing about the location. Guarding onerr == nilis correct. -
DiscardusednewPos >= len(r.buf), so discarding exactly the remaining bytes reportedio.EOFeven though allnbytes were successfully discarded.>is right and matches both stdlibbufio.Reader.Discardsemantics and the siblingbufferedReader.Discardin this same file.
No regression risk on the EOF change: the only caller (page_reader.go) ignores Discard's return values. Both tests distinguish pre- from post-fix behavior. LGTM.
byteReaderupdated its cached position even when the underlying seek failed, which could makePeekandReaddisagree about where the reader was.Discardalso returnedio.EOFwhen it consumed exactly the remaining bytes.This only updates the cached position after a successful seek and treats exact exhaustion as a successful discard. Added focused regressions for both behaviors.
Tests:
go test ./internal/utils