Skip to content

Commit 543bad8

Browse files
authored
RUST-2241 Convert panic from malformed input to error (#568)
1 parent 25ac200 commit 543bad8

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

serde-tests/test.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use bson::{
2121
cstr,
2222
doc,
2323
oid::ObjectId,
24+
serde_helpers::Utf8LossyDeserialization,
2425
spec::BinarySubtype,
2526
Binary,
2627
Bson,
@@ -1330,3 +1331,10 @@ fn invalid_length() {
13301331
// This is a regression test for fuzzer-generated input (RUST-1240).
13311332
assert!(bson::deserialize_from_slice::<Document>(&[4, 0, 0, 128, 0, 87]).is_err());
13321333
}
1334+
1335+
#[test]
1336+
fn code_with_scope_too_long() {
1337+
// This is a regression test for fuzzer-generated input (RUST-2241).
1338+
let bytes = base64::decode("KAAAAAsBCRwPAAAACwFAAAAEAA8AEAAAAAYAAAAA9wD5/wAABgALAA==").unwrap();
1339+
assert!(bson::deserialize_from_slice::<Utf8LossyDeserialization<Document>>(&bytes).is_err());
1340+
}

src/raw/iter.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,9 @@ impl<'a> RawElement<'a> {
289289
let slice = self.slice();
290290
let code = String::from_utf8_lossy(read_lenencode_bytes(&slice[4..])?).into_owned();
291291
let scope_start = 4 + 4 + code.len() + 1;
292+
if scope_start >= slice.len() {
293+
return Err(self.malformed_error("code with scope length overrun"));
294+
}
292295
let scope = RawDocument::decode_from_bytes(&slice[scope_start..])?;
293296

294297
Utf8LossyBson::JavaScriptCodeWithScope(Utf8LossyJavaScriptCodeWithScope {

0 commit comments

Comments
 (0)