reader: Fix bits_read sync on all seek variants#661
Open
PedroDeSanti wants to merge 4 commits into
Open
Conversation
There was a problem hiding this comment.
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| let (_, ret_read) = SeekStartZero::from_bytes((&input, 0)).unwrap(); | ||
| assert_eq!(ret_read, expected); | ||
| } |
| self.inner.seek(pos) | ||
|
|
||
| let new_pos = self.inner.seek(pos)?; | ||
| self.bits_read = (new_pos as usize) * 8; |
4f4f870 to
c43a3c0
Compare
wcampbell0x2a
approved these changes
Jun 18, 2026
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.
Closes #606.
Bug
Reader::seekupdatesbits_readonly for someSeekFromvariants:SeekFrom::Start(n)skips the update whenn == 0SeekFrom::Current(n)skips whenn <= 0SeekFrom::End(_)never updates itAfter one of those seeks, the inner reader sits at the right byte position but
bits_readkeeps the old value. The generatedfrom_bytesbody then computesidx = bits_read / 8and slicesinput.get(idx..)at the wrong offset. The result is either:Err(Incomplete(NeedSize { bits: N }))even though the parse already succeeded, orOk(...)with a wrong consumed-byte count.Fix
Drop the
SeekFrommatch and read the position back from the inner reader directly:Covers
Start(0), negativeCurrent, and anyEndthe same way. The saturating conversion keepsbits_readcorrect on targets whereusizeis narrower thanu64and for streams larger thanusize::MAX / 8bytes.Tests
New regression tests in
tests/test_seek.rs:test_seek_from_start_zero_rewind- round-trips a struct usingseek_from_start = "0"after a forward seek (minimal repro of the bug).test_seek_from_end_rewind- round-trips a struct usingseek_from_end, the variant that was never updatingbits_readbefore the fix.test_seek_from_current_negative_rewind- round-trips a struct using negativeseek_from_current(original scenario in Reader::seek doesn't rewind bits read, causing leftover slice error #606).test_seek_error_preserves_bits_read- directly assertsbits_readis unchanged when the inner seek returns an error.Repro
I put together a small repo with three scenarios from #606 showing upstream vs patched outputs side by side:
https://github.com/PedroDeSanti/deku-seek-bug-tests