bincode icon indicating copy to clipboard operation
bincode copied to clipboard

deserialize_from() fails when deserialize() works

Open joske opened this issue 1 year ago • 1 comments

Hi,

I'm running into an issue when deserializing from a reader. When reading the bytes into a slice first, and deserialzing that, it's working. We're serializing a struct with 3 fields, and the parts deserialize correctly with deserialize_from().

Reproduction code: https://github.com/joske/snarkVM/blob/a43dfaa9dd7f45a025050253ac149769de37d207/console/account/src/signature/serialize.rs#L89

This fails with: Error: invalid type: byte array, expected a valid signature

Working code with slice: https://github.com/joske/snarkVM/blob/a43dfaa9dd7f45a025050253ac149769de37d207/console/account/src/signature/serialize.rs#L101

This is with bincode 1.3.3. I've tried playing with the Options but it doesn't make any difference.

joske avatar Apr 25 '23 13:04 joske

I just ran into the same issue deserializing bytes. Here's an example, which works with bincode::derserialize but fails with bincode::deserialize_with:

use std::fs::File;

use serde::{Serialize, Deserialize, Serializer, Deserializer, de::Error};

#[derive(Debug, Clone, PartialEq)]
struct Bytes([u8; 64]);

impl Serialize for Bytes {
    fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        Serialize::serialize(&self.0.as_ref(), s)
    }
}

impl<'de> Deserialize<'de> for Bytes {
    fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        let slice: &[u8] = Deserialize::deserialize(d)?;
        let array: [u8; 64] = slice
            .try_into()
            .map_err(|_| D::Error::invalid_length(slice.len(), &"[u8; 64]"))?;
        Ok(Self(array))
    }
}

fn main() {
    let f = Bytes([0u8; 64]);
    let file = File::create("data").unwrap();
    bincode::serialize_into(file, &f).unwrap();

    let file = File::open("data").unwrap();
    let f_res: Bytes = bincode::deserialize_from(file).unwrap();

    assert_eq!(f, f_res);
}

I get the error

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: 
Custom("invalid type: byte array, expected a borrowed byte array")', src/main.rs:30:56

Seems like an issue with visit_bytes() being buggy for IoReader?

winston-h-zhang avatar Jun 04 '23 21:06 winston-h-zhang