Skip to content

Commit 03d31b8

Browse files
committed
[#64] Compatible if time is nagative
1 parent a94291b commit 03d31b8

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/decoder/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,11 @@ fn decode_bson<R: Read + ?Sized>(reader: &mut R, tag: u8, utf8_lossy: bool) -> D
206206
// The int64 is UTC milliseconds since the Unix epoch.
207207
let time = read_i64(reader)?;
208208

209-
match Utc.timestamp_opt(time / 1000, ((time % 1000) as u32) * 1_000_000) {
209+
let sec = time / 1000;
210+
let tmp_msec = time % 1000;
211+
let msec = if tmp_msec < 0 { 1000 - tmp_msec } else { tmp_msec };
212+
213+
match Utc.timestamp_opt(sec, (msec as u32) * 1_000_000) {
210214
LocalResult::None => Err(DecoderError::InvalidTimestamp(time)),
211215
LocalResult::Ambiguous(..) => Err(DecoderError::AmbiguousTimestamp(time)),
212216
LocalResult::Single(t) => Ok(Bson::UtcDatetime(t)),

tests/modules/encoder_decoder.rs

+14
Original file line numberDiff line numberDiff line change
@@ -296,3 +296,17 @@ fn test_decode_utc_date_time_overflows() {
296296
let expected = doc! { "A" => Utc.timestamp(1530492218, 999 * 1_000_000)};
297297
assert_eq!(decoded, expected);
298298
}
299+
300+
#[test]
301+
fn test_decode_invalid_utf8_string_issue64() {
302+
let buffer = b"\x13\x00\x00\x00\x02\x01\x00\x00\x00\x00\x00\x00\x00foo\x00\x13\x05\x00\x00\x00";
303+
304+
assert!(decode_document(&mut Cursor::new(buffer)).is_err());
305+
}
306+
307+
#[test]
308+
fn test_decode_multiply_overflows_issue64() {
309+
let buffer = b"*\xc9*\xc9\t\x00\x00\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\xca\x01\t\x00\x00\x01\x10";
310+
311+
assert!(decode_document(&mut Cursor::new(&buffer[..])).is_err());
312+
}

0 commit comments

Comments
 (0)