Skip to content

Commit

Permalink
fix: deserialize into Bytes bug
Browse files Browse the repository at this point in the history
  • Loading branch information
liuq19 committed Mar 12, 2024
1 parent e17aa97 commit be57caa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/serde/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// The code is cloned from [serde_json](https://github.com/serde-rs/json) and modified necessary parts.
use std::{mem::ManuallyDrop, ptr::slice_from_raw_parts};

use ::serde::{
use serde::{
de::{self, Expected, Unexpected},
forward_to_deserialize_any,
};
Expand Down Expand Up @@ -472,7 +472,10 @@ impl<'de, 'a, R: Reader<'de>> de::Deserializer<'de> for &'a mut Deserializer<R>
Reference::Borrowed(b) => visitor.visit_borrowed_bytes(b),
Reference::Copied(b) => visitor.visit_bytes(b),
},
b'[' => self.deserialize_seq(visitor),
b'[' => {
self.parser.read.backward(1);
self.deserialize_seq(visitor)
}
_ => Err(self.peek_invalid_type(peek, &visitor)),
};

Expand Down
18 changes: 18 additions & 0 deletions src/serde/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub use self::{
mod test {
use std::{borrow::Cow, collections::HashMap, hash::Hash, marker::PhantomData};

use bytes::Bytes;
use faststr::FastStr;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -306,6 +307,11 @@ mod test {
assert_eq!(sv, jv);
}
Err(err) => {
println!(
"parse invalid json {:?} failed for type {}",
$data,
stringify!($ty)
);
let _ = crate::from_slice::<$ty>($data).expect_err(&format!(
"parse invalid json {:?} wrong for type {}, should error: {}",
$data,
Expand All @@ -317,6 +323,12 @@ mod test {
};
}

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
pub struct Data {
#[serde(with = "serde_bytes")]
pub content: Vec<u8>,
}

// the testcase is found by fuzzing tests
#[test]
fn test_more_structs() {
Expand All @@ -326,5 +338,11 @@ mod test {
test_struct!(String, &[34, 92, 34, 34]);
test_struct!(String, b"\"\\umap9map009\"");
test_struct!(Foo, &b"[\"5XXXXXXZX:XXZX:[\",-0]"[..]);
test_struct!(
Bytes,
&b"[104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]"[..]
);
test_struct!(Bytes, &b"[]"[..]);
test_struct!(Data, &br#"{"content":[1,2,3,4,5]}"#[..]);
}
}

0 comments on commit be57caa

Please sign in to comment.