From 5ef73e37e6e9451184bcdee68fc03f3790d19905 Mon Sep 17 00:00:00 2001 From: Riccardo Casatta Date: Fri, 20 Oct 2023 10:14:25 +0200 Subject: [PATCH 1/2] deserialize_bytes data greater than scratch --- ciborium/src/de/mod.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/ciborium/src/de/mod.rs b/ciborium/src/de/mod.rs index 1952a4b..dbc623f 100644 --- a/ciborium/src/de/mod.rs +++ b/ciborium/src/de/mod.rs @@ -366,9 +366,13 @@ where 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]) + } } Header::Array(len) => self.recurse(|me| { From 6ac789d3a4bb64e4af91a7541a9e3aeec482c2a2 Mon Sep 17 00:00:00 2001 From: Riccardo Casatta Date: Fri, 20 Oct 2023 10:23:45 +0200 Subject: [PATCH 2/2] tests: deserialize_byte with data greater than 4k --- ciborium/tests/codec.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/ciborium/tests/codec.rs b/ciborium/tests/codec.rs index d737059..d601ba0 100644 --- a/ciborium/tests/codec.rs +++ b/ciborium/tests/codec.rs @@ -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); + + impl Serialize for Wrap { + fn serialize(&self, s: S) -> Result { + s.serialize_bytes(&self.0[..]) + } + } + + impl<'de> Deserialize<'de> for Wrap { + fn deserialize>(d: D) -> Result { + 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(self, v: &[u8]) -> Result { + 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); +}