Skip to content

Commit 3358916

Browse files
authored
Fix read_document_bytes unchecked conversion bug (#1274)
1 parent b1490b5 commit 3358916

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/bson_util.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,14 @@ fn num_decimal_digits(mut n: usize) -> usize {
158158

159159
/// Read a document's raw BSON bytes from the provided reader.
160160
pub(crate) fn read_document_bytes<R: Read>(mut reader: R) -> Result<Vec<u8>> {
161-
let length = reader.read_i32_sync()?;
161+
let length = Checked::new(reader.read_i32_sync()?);
162162

163-
let mut bytes = Vec::with_capacity(length as usize);
164-
bytes.write_all(&length.to_le_bytes())?;
163+
let mut bytes = Vec::with_capacity(length.try_into()?);
164+
bytes.write_all(&length.try_into::<u32>()?.to_le_bytes())?;
165165

166-
reader.take(length as u64 - 4).read_to_end(&mut bytes)?;
166+
reader
167+
.take((length - 4).try_into()?)
168+
.read_to_end(&mut bytes)?;
167169

168170
Ok(bytes)
169171
}

0 commit comments

Comments
 (0)