bincode-org / bincode

A binary encoder / decoder implementation in Rust.
MIT License
2.67k stars 265 forks source link

Unclear why serialized_size() yields different results #690

Closed henkery closed 7 months ago

henkery commented 9 months ago

context: I am using bincode 1.3.3 in helping me parse a binary format that has mixed endianness when creating my own bincode from the default options I was suddenly struck with a error regarding trailing bytes. after some experimenting I find that creating a bincode from defaultoptions it expects a different amount of bytes to deserialize the same struct as when using the bincode::deserialize

I find that the following code:

let mut bincoder = bincode::DefaultOptions::new();
let mut reverse_bincoder = bincode::DefaultOptions::new();
bincoder.with_fixint_encoding();
reverse_bincoder.with_big_endian();
bincoder.with_little_endian();
reverse_bincoder.with_varint_encoding();

#[derive(Clone, Deserialize, Serialize)]
struct MacromediaFileHeader {
    file_size: u32,
    file_sign: [u8; 4], // CP1252 string
    imap: [u8; 4], // CP1252 string
    imap_length: u32,
    imap_unknown: u32,
    mmap_offset: u32,
}

let num = reverse_bincoder.serialized_size(&MacromediaFileHeader { file_sign: [0,0,0,0], file_size: 0, imap: [0,0,0,0], imap_length: 0, imap_unknown: 0, mmap_offset: 0}).unwrap();
let num3 = bincoder.serialized_size(&MacromediaFileHeader { file_sign: [0,0,0,0], file_size: 0, imap: [0,0,0,0], imap_length: 0, imap_unknown: 0, mmap_offset: 0}).unwrap();
let num3 = bincode::serialized_size(&MacromediaFileHeader { file_sign: [0,0,0,0], file_size: 0, imap: [0,0,0,0], imap_length: 0, imap_unknown: 0, mmap_offset: 0}).unwrap();

yields: num: 12 num2: 12 num3: 24

I experimented a bit and crawled though the docs but I cannot seem to understand what's going wrong here

stale[bot] commented 7 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.