From e838d3e04b565604ea4878979716a775e76f8f3e Mon Sep 17 00:00:00 2001 From: Patrick Meredith Date: Tue, 31 Dec 2024 14:01:50 -0500 Subject: [PATCH] Fix read_document_bytes unchecked conversion bug --- src/bson_util.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/bson_util.rs b/src/bson_util.rs index 4a5aeae9e..d15d2b670 100644 --- a/src/bson_util.rs +++ b/src/bson_util.rs @@ -158,12 +158,14 @@ fn num_decimal_digits(mut n: usize) -> usize { /// Read a document's raw BSON bytes from the provided reader. pub(crate) fn read_document_bytes(mut reader: R) -> Result> { - let length = reader.read_i32_sync()?; + let length = Checked::new(reader.read_i32_sync()?); - let mut bytes = Vec::with_capacity(length as usize); - bytes.write_all(&length.to_le_bytes())?; + let mut bytes = Vec::with_capacity(length.try_into()?); + bytes.write_all(&length.try_into::()?.to_le_bytes())?; - reader.take(length as u64 - 4).read_to_end(&mut bytes)?; + reader + .take((length - 4).try_into()?) + .read_to_end(&mut bytes)?; Ok(bytes) }