Skip to content

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

Open
@bentheiii

Description

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

Activity

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

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions