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_bytes with data bigger than 4k #97

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions ciborium/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,13 @@
return match self.decoder.pull()? {
Header::Tag(..) => continue,

Header::Bytes(Some(len)) if len <= self.scratch.len() => {
self.decoder.read_exact(&mut self.scratch[..len])?;
visitor.visit_bytes(&self.scratch[..len])
Header::Bytes(Some(len)) => {
if len <= self.scratch.len() {
self.decoder.read_exact(&mut self.scratch[..len])?;
visitor.visit_bytes(&self.scratch[..len])
} else {
visitor.visit_byte_buf(vec![0u8; len])

Check failure on line 374 in ciborium/src/de/mod.rs

View workflow job for this annotation

GitHub Actions / test beta debug ciborium

cannot find macro `vec` in this scope

Check failure on line 374 in ciborium/src/de/mod.rs

View workflow job for this annotation

GitHub Actions / test 1.70.0 release ciborium

cannot find macro `vec` in this scope

Check failure on line 374 in ciborium/src/de/mod.rs

View workflow job for this annotation

GitHub Actions / test 1.70.0 debug ciborium

cannot find macro `vec` in this scope

Check failure on line 374 in ciborium/src/de/mod.rs

View workflow job for this annotation

GitHub Actions / test beta release ciborium

cannot find macro `vec` in this scope

Check failure on line 374 in ciborium/src/de/mod.rs

View workflow job for this annotation

GitHub Actions / test nightly debug ciborium

cannot find macro `vec` in this scope

Check failure on line 374 in ciborium/src/de/mod.rs

View workflow job for this annotation

GitHub Actions / test nightly release ciborium

cannot find macro `vec` in this scope

Check failure on line 374 in ciborium/src/de/mod.rs

View workflow job for this annotation

GitHub Actions / test stable release ciborium

cannot find macro `vec` in this scope

Check failure on line 374 in ciborium/src/de/mod.rs

View workflow job for this annotation

GitHub Actions / test stable debug ciborium

cannot find macro `vec` in this scope
}
}

Header::Array(len) => self.recurse(|me| {
Expand Down
40 changes: 40 additions & 0 deletions ciborium/tests/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,3 +436,43 @@ fn handle_struct_field_names(input: &str, expected: Foo) {
let read = from_reader(&buf[..]).unwrap();
assert_eq!(expected, read);
}

#[test]
fn deserialize_bytes_greater_than_4k() {
#[derive(PartialEq, Eq, Debug)]
pub struct Wrap(Vec<u8>);

impl Serialize for Wrap {
fn serialize<S: ::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
s.serialize_bytes(&self.0[..])
}
}

impl<'de> Deserialize<'de> for Wrap {
fn deserialize<D: ::serde::Deserializer<'de>>(d: D) -> Result<Wrap, D::Error> {
d.deserialize_bytes(WrapVisitor {})
}
}

pub struct WrapVisitor;

impl<'de> serde::de::Visitor<'de> for WrapVisitor {
type Value = Wrap;

fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
formatter.write_str("oh no!")
}

fn visit_bytes<E: serde::de::Error>(self, v: &[u8]) -> Result<Self::Value, E> {
Ok(Wrap(v.to_vec()))
}
}

let a = Wrap(vec![0u8; 5000]);

let mut bytes = vec![];
ciborium::ser::into_writer(&a, &mut bytes).unwrap();

let b: Wrap = ciborium::from_reader(&bytes[..]).unwrap();
assert_eq!(a, b);
}
Loading