jam1garner / jam1garner/binrw

[BUG] Struct with Option set to None fails round trip test

Open
#363 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

confusing api
Dominant language
Rust
Stars
853
Forks
56
PR merge metrics
No merged PRs in 30d

Description

Description

When using a Optional field in a struct and setting it to None something bad happens. I have seen even wrong decodes where it was set to Some(..) or as shown in my example below it just fails to decode.

Affected Version

Latest (0.15.1)

Example

#[derive(BinRead, BinWrite, Debug, PartialEq)]
pub struct Foo {
    pub bar: Option<u8>,
}

#[cfg(test)]
mod tests {
    use std::io::Cursor;

    use super::*;

    #[test]
    fn round_trip_filled() {
        let foo = Foo { bar: Some(1) };

        let mut writer = Cursor::new(Vec::new());
        foo.write_le(&mut writer).unwrap();

        let mut reader = Cursor::new(writer.into_inner());
        let decoded = Foo::read_le(&mut reader).unwrap();

        assert_eq!(foo, decoded);
    }

    #[test]
    fn round_trip_empty() {
        let foo = Foo { bar: None };

        let mut writer = Cursor::new(Vec::new());
        foo.write_le(&mut writer).unwrap();

        let mut reader = Cursor::new(writer.into_inner());
        let decoded = Foo::read_le(&mut reader).unwrap();

        assert_eq!(foo, decoded);
    }
}

Cargo test output

test tests::round_trip_filled ... ok
test tests::round_trip_empty ... FAILED

failures:

---- tests::round_trip_empty stdout ----

thread 'tests::round_trip_empty' (1336949) panicked at src/main.rs:56:49:
called `Result::unwrap()` on an `Err` value:
 ╺━━━━━━━━━━━━━━━━━━━━┅ Backtrace ┅━━━━━━━━━━━━━━━━━━━━╸

 0: Error: failed to fill whole buffer
           While parsing field 'bar' in Foo
     at src/main.rs:26

 ╺━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╸

Additional Information

I also tried using try with magic but it still fails:

#[derive(BinRead, BinWrite, Debug, PartialEq)]
pub struct Foo {
    #[bw(magic = b"bar", if(bar.is_some()))]
    #[br(magic = b"bar", try)]
    pub bar: Option<u8>,
}

#[cfg(test)]
mod tests {
    use std::io::Cursor;

    use super::*;

    #[test]
    fn round_trip_filled() {
        let foo = Foo { bar: Some(1) };

        let mut writer = Cursor::new(Vec::new());
        foo.write_le(&mut writer).unwrap();

        let bytes = writer.into_inner();
        dbg!(&bytes);

        let mut reader = Cursor::new(bytes);
        let decoded = Foo::read_le(&mut reader).unwrap();

        assert_eq!(foo, decoded);
    }

    #[test]
    fn round_trip_empty() {
        let foo = Foo { bar: None };

        let mut writer = Cursor::new(Vec::new());
        foo.write_le(&mut writer).unwrap();

        let bytes = writer.into_inner();
        dbg!(&bytes);

        let mut reader = Cursor::new(bytes);
        let decoded = Foo::read_le(&mut reader).unwrap();

        assert_eq!(foo, decoded);
    }
}
Cargo test output
test tests::round_trip_filled ... ok
test tests::round_trip_empty ... FAILED

failures:

---- tests::round_trip_empty stdout ----
[src/main.rs:61:9] &bytes = []

thread 'tests::round_trip_empty' (1353280) panicked at src/main.rs:64:49:
called `Result::unwrap()` on an `Err` value: failed to fill whole buffer
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the supplied Foo round-trip tests with Cargo test, including both Some and None cases. Trace the Option handling used by the BinRead and BinWrite derives to see why an empty value produces no readable representation. Done means both round-trip tests pass without changing the reported behavior for Some(1).

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
62/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.