Skip to content

Commit

Permalink
RUST-1240 Fix potential underflow in length counting (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
abr-egn committed Mar 30, 2022
1 parent 7362bd3 commit 176b53f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
6 changes: 6 additions & 0 deletions serde-tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1405,3 +1405,9 @@ fn non_human_readable() {
assert_eq!(human_readable, expected);
assert_eq!(human_readable, non_human_readable);
}

#[test]
fn invalid_length() {
// This is a regression test for fuzzer-generated input (RUST-1240).
assert!(bson::from_slice::<Document>(&[4, 0, 0, 128, 0, 87]).is_err());
}
8 changes: 5 additions & 3 deletions src/de/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,11 @@ impl<'de> Deserializer<'de> {
where
F: FnOnce(DocumentAccess<'_, 'de>) -> Result<O>,
{
let mut length_remaining = read_i32(&mut self.bytes)?
.checked_sub(4)
.ok_or_else(|| Error::custom("invalid length, less than min document size"))?;
let mut length_remaining = read_i32(&mut self.bytes)?;
if length_remaining < 4 {
return Err(Error::custom("invalid length, less than min document size"));
}
length_remaining -= 4;
let out = f(DocumentAccess {
root_deserializer: self,
length_remaining: &mut length_remaining,
Expand Down

0 comments on commit 176b53f

Please sign in to comment.