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

deserialize_from() fails when deserialize() works #633

Open
joske opened this issue Apr 25, 2023 · 1 comment
Open

deserialize_from() fails when deserialize() works #633

joske opened this issue Apr 25, 2023 · 1 comment

Comments

@joske
Copy link

joske commented Apr 25, 2023

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 joske changed the title deserialize_from() fails when serialize() works deserialize_from() fails when deserialize() works Apr 25, 2023
@winston-h-zhang
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants