Skip to content

Commit

Permalink
Check for overflow when parsing datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
botahamec committed May 26, 2022
1 parent bbc50f8 commit dfc4027
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/datetime/tests.rs
Expand Up @@ -130,6 +130,7 @@ fn test_datetime_rfc2822_and_rfc3339() {
DateTime::parse_from_rfc3339("2015-02-18T23:59:60.234567+05:00"),
Ok(edt.ymd(2015, 2, 18).and_hms_micro(23, 59, 59, 1_234_567))
);
assert!(DateTime::parse_from_rfc2822("31 DEC 262143 23:59 -2359").is_err());
}

#[test]
Expand Down
6 changes: 6 additions & 0 deletions src/format/parsed.rs
Expand Up @@ -626,6 +626,12 @@ impl Parsed {
let offset = self.offset.ok_or(NOT_ENOUGH)?;
let datetime = self.to_naive_datetime_with_offset(offset)?;
let offset = FixedOffset::east_opt(offset).ok_or(OUT_OF_RANGE)?;

// this is used to prevent an overflow when calling FixedOffset::from_local_datetime
datetime
.checked_sub_signed(OldDuration::seconds(i64::from(offset.local_minus_utc())))
.ok_or(OUT_OF_RANGE)?;

match offset.from_local_datetime(&datetime) {
LocalResult::None => Err(IMPOSSIBLE),
LocalResult::Single(t) => Ok(t),
Expand Down

0 comments on commit dfc4027

Please sign in to comment.