Fix id write endianness and sub-width signed sign-extension - #664
Merged
wcampbell0x2a merged 3 commits intoSep 1, 2026
Merged
Conversation
Collaborator
|
The following would panic with this patch: #[derive(Debug, DekuRead)]
struct Dynamic {
n: u8,
#[deku(bytes = "*n as usize")]
a: i32,
}
#[test]
fn dynamic_zero() {
// n = 0 -> ByteSize(0) at runtime
let mut c = Cursor::new([0x00u8, 0x01, 0x02]);
println!("{:?}", Dynamic::from_reader((&mut c, 0)));
}
} |
wcampbell0x2a
requested changes
Aug 6, 2026
wcampbell0x2a
left a comment
Collaborator
There was a problem hiding this comment.
Requesting changes, see previous comment.
Patch A (deku-derive/src/macros/deku_write.rs): Mirror gen_id_args' read-side precedence (id_endian -> enum endian) for the id_pat write path. Previously field_endian was derived from id_endian only, ignoring a hardcoded endian="big" or a bound ctx endian, causing the enum to write the id in little-endian even when the enum declared endian="big". Patch B (src/impls/primitive.rs): Sign-extend in the (Endian, ByteSize, Order) DekuReader impl for signed types (ImplDekuReadSignExtend macro). A sub-width byte-aligned signed read previously zero-padded the high bytes without propagating the sign bit, so e.g. a 3-byte i32 read of -100 returned +16776092. The fix applies the same shift-based sign-extension already used in the BitSize path. Regression tests in tests/test_enum.rs cover: - id_pat enum with endian="big" writes same bytes as flat big-endian field - signed 24-bit id_pat enum round-trips negative sentinels on LE and BE Regression tests in src/impls/primitive.rs cover the ByteSize sign-extend path directly for i32 and i16 sub-width reads on both endians.
gusanthon
force-pushed
the
fix/id-pat-endian-signext
branch
from
August 19, 2026 10:33
d6e8b0e to
28ac6fc
Compare
Contributor
Author
|
Thanks for the review, @wcampbell0x2a, and good catch. Fixed in a4debfa by skipping the sign-extension when the size is 0. Your snippet gives |
wcampbell0x2a
approved these changes
Sep 1, 2026
sharksforarms
pushed a commit
that referenced
this pull request
Sep 3, 2026
sharksforarms
pushed a commit
that referenced
this pull request
Sep 3, 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.
Summary
Two independent correctness fixes for byte-aligned reads/writes, each with
regression tests. They're unrelated bugs that happened to surface together
while implementing a binary codec - happy to split into two PRs if you'd
prefer to review/merge them separately.
Patch A - enum
idwritten with the wrong endiannessFile:
deku-derive/src/macros/deku_write.rsOn the
id_patwrite path,field_endianwas derived fromid_endianonly, ignoring both a hardcoded
endian = "big"and a bound ctx endian.As a result an enum that declares
endian = "big"would still write itsidin little-endian, so the written bytes didn't match the equivalentflat big-endian field (and didn't round-trip against the read side).
Fix: mirror the read-side precedence already used in
gen_id_args(
id_endian-> enum endian) for theid_patwrite path, so the write sideresolves endianness the same way the read side does.
Tests (
tests/test_enum.rs):id_patenum withendian = "big"writes the same bytes as theequivalent flat big-endian field
id_patenum round-trips negative sentinels on both LEand BE
Patch B - sub-width signed reads not sign-extended
File:
src/impls/primitive.rsThe
(Endian, ByteSize, Order)DekuReaderimpl for signed types(
ImplDekuReadSignExtendmacro) zero-padded the high bytes of a sub-widthbyte-aligned read without propagating the sign bit. E.g. a 3-byte
i32read of
-100returned+16776092instead of-100.Fix: apply the same shift-based sign-extension already used in the
BitSizepath, so theByteSizepath sign-extends consistently.Tests (
src/impls/primitive.rs): cover theByteSizesign-extend pathdirectly for
i32andi16sub-width reads on both endians.