Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing unit enum variants tries to rewind on a NoSeek stream #215

Closed
sigeryang opened this issue Aug 1, 2023 · 1 comment
Closed

Parsing unit enum variants tries to rewind on a NoSeek stream #215

sigeryang opened this issue Aug 1, 2023 · 1 comment
Assignees

Comments

@sigeryang
Copy link

Minimal example:

use std::io::Cursor;

use binrw::{binrw, BinRead, BinWrite, BinReaderExt, BinWriterExt, io::NoSeek};

#[derive(Debug, Copy, Clone)]
#[binrw]
#[brw(big, repr = u32)]
enum Unit {
    A = 1,
    B = 2,
    C = 3,
}

fn main() {
    let cursor = Cursor::new(b"\0\0\0\0");
    let unit = Unit::read(&mut NoSeek::new(cursor)).unwrap();
    println!("{:?}", unit);
}

Output:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: seek on unseekable file', src/main.rs:16:53
@csnover csnover self-assigned this Aug 8, 2023
csnover added a commit to csnover/binrw that referenced this issue Aug 9, 2023
csnover added a commit to csnover/binrw that referenced this issue Aug 9, 2023
csnover added a commit to csnover/binrw that referenced this issue Aug 9, 2023
@csnover
Copy link
Collaborator

csnover commented Aug 9, 2023

Thanks for your report!

In the given test case, the enumeration does not match the data, so parsing has failed. When this happens, the parser attempts to restore the position of the data stream so it is not left in an indeterminate state. This is expected behaviour. However, the original error should not be lost. I have added a patch to binrw to ensure the original error is retained when this happens, which should resolve this issue. Let me know if you have any questions about this.

@csnover csnover closed this as completed Aug 9, 2023
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

No branches or pull requests

2 participants