3Hren / 3Hren/msgpack-rust

Regression since 1.1.1: discard when reading from sequence after error

Open
#361 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
1.4k
Forks
162
PR merge metrics
No merged PRs in 30d

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:

```rust
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(deserializer: D) -> Result
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(self, mut seq: A) -> Result
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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.