Skip to content

Commit 176b53f

Browse files
committed
RUST-1240 Fix potential underflow in length counting (#349)
1 parent 7362bd3 commit 176b53f

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

serde-tests/test.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,3 +1405,9 @@ fn non_human_readable() {
14051405
assert_eq!(human_readable, expected);
14061406
assert_eq!(human_readable, non_human_readable);
14071407
}
1408+
1409+
#[test]
1410+
fn invalid_length() {
1411+
// This is a regression test for fuzzer-generated input (RUST-1240).
1412+
assert!(bson::from_slice::<Document>(&[4, 0, 0, 128, 0, 87]).is_err());
1413+
}

src/de/raw.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,11 @@ impl<'de> Deserializer<'de> {
171171
where
172172
F: FnOnce(DocumentAccess<'_, 'de>) -> Result<O>,
173173
{
174-
let mut length_remaining = read_i32(&mut self.bytes)?
175-
.checked_sub(4)
176-
.ok_or_else(|| Error::custom("invalid length, less than min document size"))?;
174+
let mut length_remaining = read_i32(&mut self.bytes)?;
175+
if length_remaining < 4 {
176+
return Err(Error::custom("invalid length, less than min document size"));
177+
}
178+
length_remaining -= 4;
177179
let out = f(DocumentAccess {
178180
root_deserializer: self,
179181
length_remaining: &mut length_remaining,

0 commit comments

Comments
 (0)