Check for overflow when parsing datetime

This commit is contained in:
Micha White 2022-05-26 15:28:24 -04:00 committed by Dirkjan Ochtman
parent 50eb7e363d
commit ad03bcbdcb
2 changed files with 7 additions and 0 deletions

View File

@ -126,6 +126,7 @@ fn test_datetime_rfc2822_and_rfc3339() {
DateTime::parse_from_rfc2822("Wed, 18 Feb 2015 23:59:60 +0500"),
Ok(edt.ymd(2015, 2, 18).and_hms_milli(23, 59, 59, 1_000))
);
assert!(DateTime::parse_from_rfc2822("31 DEC 262143 23:59 -2359").is_err());
assert_eq!(
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))

View File

@ -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),