Skip to content

Commit

Permalink
[#64] Compatible if time is nagative
Browse files Browse the repository at this point in the history
  • Loading branch information
zonyitoo committed Jul 3, 2018
1 parent a94291b commit 03d31b8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/decoder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,11 @@ fn decode_bson<R: Read + ?Sized>(reader: &mut R, tag: u8, utf8_lossy: bool) -> D
// The int64 is UTC milliseconds since the Unix epoch.
let time = read_i64(reader)?;

match Utc.timestamp_opt(time / 1000, ((time % 1000) as u32) * 1_000_000) {
let sec = time / 1000;
let tmp_msec = time % 1000;
let msec = if tmp_msec < 0 { 1000 - tmp_msec } else { tmp_msec };

match Utc.timestamp_opt(sec, (msec as u32) * 1_000_000) {
LocalResult::None => Err(DecoderError::InvalidTimestamp(time)),
LocalResult::Ambiguous(..) => Err(DecoderError::AmbiguousTimestamp(time)),
LocalResult::Single(t) => Ok(Bson::UtcDatetime(t)),
Expand Down
14 changes: 14 additions & 0 deletions tests/modules/encoder_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,17 @@ fn test_decode_utc_date_time_overflows() {
let expected = doc! { "A" => Utc.timestamp(1530492218, 999 * 1_000_000)};
assert_eq!(decoded, expected);
}

#[test]
fn test_decode_invalid_utf8_string_issue64() {
let buffer = b"\x13\x00\x00\x00\x02\x01\x00\x00\x00\x00\x00\x00\x00foo\x00\x13\x05\x00\x00\x00";

assert!(decode_document(&mut Cursor::new(buffer)).is_err());
}

#[test]
fn test_decode_multiply_overflows_issue64() {
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";

assert!(decode_document(&mut Cursor::new(&buffer[..])).is_err());
}

0 comments on commit 03d31b8

Please sign in to comment.