Skip to content

RUST-882 Fix lossy From<u32> Bson impl, prevent overflow in From<u64> (1.2.x) #281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/bson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
//! BSON definition

use std::{
convert::{TryFrom, TryInto},
fmt::{self, Debug, Display},
ops::{Deref, DerefMut},
};
Expand Down Expand Up @@ -266,13 +267,22 @@ impl From<i64> for Bson {

impl From<u32> for Bson {
fn from(a: u32) -> Bson {
Bson::Int32(a as i32)
if let Ok(i) = i32::try_from(a) {
Bson::Int32(i)
} else {
Bson::Int64(a.into())
}
}
}

impl From<u64> for Bson {
/// This conversion is lossy if the provided `u64` is greater than `i64::MAX`, which it will be
/// converted to. Using this `From` implementation is highly discouraged and it will be
/// removed in the next major version.
///
/// Note: due to https://github.com/rust-lang/rust/issues/39935 we cannot deprecate this implementation.
fn from(a: u64) -> Bson {
Bson::Int64(a as i64)
Bson::Int64(a.try_into().unwrap_or(i64::MAX))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for lack of a better option, I tried to make this "less bad", but short of panicking I'm not sure we have any other choice. I'm hesitant to panic given that we can't even deprecate this impl though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed that this is the less-bad option.

}
}

Expand Down
4 changes: 1 addition & 3 deletions src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,7 @@ fn read_f128<R: Read + ?Sized>(reader: &mut R) -> Result<Decimal128> {
#[cfg(feature = "decimal128")]
#[inline]
fn read_f128<R: Read + ?Sized>(reader: &mut R) -> Result<Decimal128> {
use std::mem;

let mut local_buf: [u8; 16] = unsafe { mem::MaybeUninit::uninit().assume_init() };
let mut local_buf = [0u8; 16];
reader.read_exact(&mut local_buf)?;
let val = unsafe { Decimal128::from_raw_bytes_le(local_buf) };
Ok(val)
Expand Down
4 changes: 4 additions & 0 deletions src/tests/modules/bson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ fn from_impls() {
assert_eq!(Bson::from(-48i32), Bson::Int32(-48));
assert_eq!(Bson::from(-96i64), Bson::Int64(-96));
assert_eq!(Bson::from(152u32), Bson::Int32(152));
assert_eq!(
Bson::from(i32::MAX as u32 + 1),
Bson::Int64(i32::MAX as i64 + 1)
);
assert_eq!(Bson::from(4096u64), Bson::Int64(4096));

let oid = ObjectId::new();
Expand Down
4 changes: 2 additions & 2 deletions src/tests/modules/ordered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ fn test_getters() {

doc.insert("null".to_string(), Bson::Null);
assert_eq!(Some(&Bson::Null), doc.get("null"));
assert_eq!(true, doc.is_null("null"));
assert_eq!(false, doc.is_null("array"));
assert!(doc.is_null("null"));
assert!(!doc.is_null("array"));

assert_eq!(Some(&Bson::Int32(1)), doc.get("i32"));
assert_eq!(Ok(1i32), doc.get_i32("i32"));
Expand Down
2 changes: 1 addition & 1 deletion src/tests/modules/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn boolean() {
let _guard = LOCK.run_concurrently();
let obj = Bson::Boolean(true);
let b: bool = from_bson(obj.clone()).unwrap();
assert_eq!(b, true);
assert!(b);

let deser: Bson = to_bson(&b).unwrap();
assert_eq!(deser, obj);
Expand Down