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

Regression since 1.1.1: discard when reading from sequence after error #361

Open
bentheiii opened this issue Feb 10, 2025 · 0 comments · May be fixed by #362
Open

Regression since 1.1.1: discard when reading from sequence after error #361

bentheiii opened this issue Feb 10, 2025 · 0 comments · May be fixed by #362

Comments

@bentheiii
Copy link

bentheiii commented Feb 10, 2025

Hello, I noted a regression when using rmp_serde 1.2.0 and over, when trying to read from a deserializer that had an error

consider the following script:

use std::fmt;

use rmp_serde;
use serde::Deserialize;
use serde::de::{Deserializer, Visitor};

// a sample struct that expects an array with one i8 at the end
// to simulate error recovery
struct ContinueTillI8(i8);

impl<'de> Deserialize<'de> for ContinueTillI8 {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct ContinueTillI8Visitor;

        impl<'de> Visitor<'de> for ContinueTillI8Visitor {
            type Value = ContinueTillI8;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("array that has one i8 at the end")
            }

            fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
                where
                    A: serde::de::SeqAccess<'de>, {
                loop {
                    match seq.next_element() {
                        Ok(Some(i)) => {
                            return Ok(ContinueTillI8(i));
                        }
                        Err(e) => {
                            println!("discraded element with err: {:?}", e);
                        }
                        Ok(None) => {
                            return Err(serde::de::Error::custom("No i8 found"));
                        }
                    }
                }
            }
        }

        deserializer.deserialize_any(ContinueTillI8Visitor)
    }
}

fn main(){
    let data = ("hi", 8);
    let raw_data = rmp_serde::to_vec(&data).unwrap();

    let value: ContinueTillI8 = rmp_serde::from_slice(&raw_data[..]).unwrap();

    println!("{:?}", value.0); // should print 8
}

with rmp_serde 1.2.0 and up, I get the following result:

discarded element with err: TypeMismatch(FixStr(2))
104

with 1.1.1, we get the expected output of 8

discarded element with err: Syntax("invalid type: string \"hi\", expected i8")
8

this is likely due to rmp_serde not consuming the FixStr(2)'s actual value after detecting it is not an i8, leaving the deserailizer in an invalid state

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

Successfully merging a pull request may close this issue.

1 participant