Skip to content

Conversation

@amboar
Copy link
Contributor

@amboar amboar commented Nov 24, 2025

Addresses the observation at #617 (comment)

@amboar amboar changed the title Misaligned skip bits seek Fix misaligned seek in Reader::skip_bits() Nov 24, 2025
Prior to the fix, the newly added test case yields:

    $ RUST_LOG=trace cargo test --no-default-features --features=alloc,bits,logging --test test_lsb_le -- test_misaligned_pad_bits_aaelf32_flags
        Finished `test` profile [unoptimized + debuginfo] target(s) in 0.03s
         Running tests/test_lsb_le.rs (target/debug/deps/test_lsb_le-f72cea0c554eeb76)

    running 1 test
    [2025-11-24T01:58:46Z TRACE deku::reader] skip_bits: 9
    [2025-11-24T01:58:46Z TRACE deku::reader] seek: Current(1)
    [2025-11-24T01:58:46Z TRACE deku::reader] read_bits_into: Lsb0, 1
    [2025-11-24T01:58:46Z TRACE test_lsb_le] Reading: AaElf32Flags.ef_arm_abi_float_soft
    [2025-11-24T01:58:46Z TRACE deku::reader] read_bits_into: Lsb0, 1
    [2025-11-24T01:58:46Z TRACE test_lsb_le] Reading: AaElf32Flags.ef_arm_abi_float_hard
    [2025-11-24T01:58:46Z TRACE deku::reader] read_bits_into: Lsb0, 1
    [2025-11-24T01:58:46Z TRACE deku::reader] skip_bits: 11
    [2025-11-24T01:58:46Z TRACE deku::reader] read_bits_into: Lsb0, 3
    [2025-11-24T01:58:46Z TRACE deku::reader] seek: Current(1)
    [2025-11-24T01:58:46Z TRACE deku::reader] read_bits_into: Lsb0, 0
    [2025-11-24T01:58:46Z TRACE test_lsb_le] Reading: AaElf32Flags.ef_arm_gccmask
    [2025-11-24T01:58:46Z TRACE deku::reader] read_bits_into: Lsb0, 1
    [2025-11-24T01:58:46Z TRACE test_lsb_le] Reading: AaElf32Flags.ef_arm_be8
    [2025-11-24T01:58:46Z TRACE deku::reader] read_bits_into: Lsb0, 1
    [2025-11-24T01:58:46Z TRACE test_lsb_le] Reading: AaElf32Flags.ef_arm_abimask
    [2025-11-24T01:58:46Z TRACE deku::reader] read_bits_into: Lsb0, 8
    test test_misaligned_pad_bits_aaelf32_flags ... FAILED

This boils down to an seek where we're not yet byte-aligned. The
addition of:

    @@ -207,6 +207,7 @@ impl<R: Read + Seek> Reader<R> {

                 // first, seek with bytes
                 if bytes_amt != 0 {
    +                debug_assert_eq!(self.bits_read % 8, 0);
                     self.seek(SeekFrom::Current(
                         i64::try_from(bytes_amt).expect("could not convert seek usize into i64"),
                     ))

yields:

    $ cargo test --no-default-features --features=alloc,bits,logging --test test_lsb_le -- test_misaligned_pad_bits_aaelf32_flags
        Finished `test` profile [unoptimized + debuginfo] target(s) in 0.03s
         Running tests/test_lsb_le.rs (target/debug/deps/test_lsb_le-f72cea0c554eeb76)

    running 1 test
    test test_misaligned_pad_bits_aaelf32_flags ... FAILED

    failures:

    ---- test_misaligned_pad_bits_aaelf32_flags stdout ----

    thread 'test_misaligned_pad_bits_aaelf32_flags' (131091) panicked at /home/andrew/src/github.com/sharksforarms/deku/src/reader.rs:210:17:
    assertion `left == right` failed
      left: 6
     right: 0
    note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

    failures:
        test_misaligned_pad_bits_aaelf32_flags

    test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 8 filtered out; finished in 0.00s

Account for alignment in Reader::skip_bits() prior to deriving the range
over which to seek.

Signed-off-by: Andrew Jeffery <andrew@codeconstruct.com.au>
@amboar amboar force-pushed the misaligned-skip-bits-seek branch from f1a14c2 to 6a3e7e5 Compare November 24, 2025 04:04
Copy link
Owner

@sharksforarms sharksforarms left a comment

Choose a reason for hiding this comment

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

Thank you!

@wcampbell0x2a wcampbell0x2a merged commit 015c650 into sharksforarms:master Nov 24, 2025
10 checks passed
@github-actions github-actions bot mentioned this pull request Nov 24, 2025
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