3Hren / msgpack-rust

MessagePack implementation for Rust / msgpack.org[Rust]
MIT License
1.17k stars 130 forks source link

rmp_serde fails to fill buffer from file when decoding #315

Closed gluax closed 2 years ago

gluax commented 2 years ago
let mut msgpack = std::fs::OpenOptions::new()
        .read(true)
        .write(true)
        .create(true)
        .open("foo.msgpack")
        .expect("f");

    rmp_serde::encode::write(&mut msgpack, &132).expect("write");
    let num: i32 = rmp_serde::decode::from_read(&mut msgpack).expect("read");
    dbg!(num);

Results in read: InvalidMarkerRead(Error { kind: UnexpectedEof, message: "failed to fill whole buffer" }).

Happens with structs as well, but just wanted to simplify the issue.

kornelski commented 2 years ago

It means it expected there to be more data. It may happen if msgpack file was created with a different version or has a structure that doesn't map to the type you're deserializing.

gluax commented 2 years ago

I'm using the same version in the above example, and the types are explicit. So I don't understand what could be going wrong. If I'm doing something wrong, please point it out because I certainly don't see why the above code would be invalid :c.

kornelski commented 2 years ago

Oh, I see. You're trying to read past the end of the file. Reading doesn't rewind. It continues after position of last write, where there is nothing.

Reopen or rewind the file.

gluax commented 2 years ago

That was silly of me! Thanks for that!