Skip to content

Commit

Permalink
Check that reader empty
Browse files Browse the repository at this point in the history
  • Loading branch information
preston-evans98 committed Sep 9, 2023
1 parent 0ba5d80 commit e4f1861
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
35 changes: 24 additions & 11 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ where
T: DeserializeOwned,
{
let mut deserializer = Deserializer::from_reader(reader, crate::MAX_CONTAINER_DEPTH);
T::deserialize(&mut deserializer)
let t = T::deserialize(&mut deserializer)?;
deserializer.end().map(move |_| t)
}

/// Deserialize a type from an implementation of [`Read`] using the provided seed
Expand All @@ -71,7 +72,8 @@ where
for<'a> T: DeserializeSeed<'a>,
{
let mut deserializer = Deserializer::from_reader(reader, crate::MAX_CONTAINER_DEPTH);
seed.deserialize(&mut deserializer)
let t = seed.deserialize(&mut deserializer)?;
deserializer.end().map(move |_| t)
}

/// Deserialization implementation for BCS
Expand Down Expand Up @@ -146,6 +148,11 @@ trait BcsDeserializer<'de> {
seed: K,
) -> Result<(K::Value, Self::MaybeBorrowedBytes), Error>;

/// The `Deserializer::end` method should be called after a type has been
/// fully deserialized. This allows the `Deserializer` to validate that
/// the there are no more bytes remaining in the input stream.
fn end(&mut self) -> Result<()>;

fn parse_bool(&mut self) -> Result<bool> {
let byte = self.next()?;

Expand Down Expand Up @@ -266,6 +273,15 @@ impl<'de, R: Read> BcsDeserializer<'de> for Deserializer<TeeReader<'de, R>> {
let key_bytes = self.input.capture_buffer.take().unwrap();
Ok((key_value, key_bytes))
}

fn end(&mut self) -> Result<()> {
let mut byte = [0u8; 1];
match self.input.read_exact(&mut byte) {
Ok(_) => Err(Error::RemainingInput),
Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => Ok(()),
Err(e) => Err(e.into()),
}
}
}

impl<'de> BcsDeserializer<'de> for Deserializer<&'de [u8]> {
Expand Down Expand Up @@ -307,23 +323,20 @@ impl<'de> BcsDeserializer<'de> for Deserializer<&'de [u8]> {
let key_bytes = &previous_input_slice[..key_len];
Ok((key_value, key_bytes))
}
}

impl<'de> Deserializer<&'de [u8]> {
fn peek(&mut self) -> Result<u8> {
self.input.first().copied().ok_or(Error::Eof)
}

/// The `Deserializer::end` method should be called after a type has been
/// fully deserialized. This allows the `Deserializer` to validate that
/// the there are no more bytes remaining in the input stream.
fn end(&mut self) -> Result<()> {
if self.input.is_empty() {
Ok(())
} else {
Err(Error::RemainingInput)
}
}
}

impl<'de> Deserializer<&'de [u8]> {
fn peek(&mut self) -> Result<u8> {
self.input.first().copied().ok_or(Error::Eof)
}

fn parse_bytes(&mut self) -> Result<&'de [u8]> {
let len = self.parse_length()?;
Expand Down
4 changes: 4 additions & 0 deletions tests/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,10 @@ fn by_default_btreesets_are_serialized_as_sequences() {
fn leftover_bytes() {
let seq = vec![5, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // 5 extra elements
assert_eq!(from_bytes::<Vec<u8>>(&seq), Err(Error::RemainingInput));
assert_eq!(
from_bytes_via_reader::<Vec<u8>>(&seq),
Err(Error::RemainingInput)
);
}

#[test]
Expand Down

0 comments on commit e4f1861

Please sign in to comment.