diff --git a/CHANGELOG.md b/CHANGELOG.md index 39174ef312..9292b02eb2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ Versions with only mechanical changes will be omitted from the following list. * Add support for optional timestamps serde serialization for `NaiveDateTime`. * Fix build for wasm32-unknown-emscripten (@yu-re-ka #593) * Implement `DoubleEndedIterator` for `NaiveDateDaysIterator` and `NaiveDateWeeksIterator` +* Fix panicking when parsing a `DateTime` (@botahamec) ## 0.4.19 diff --git a/src/datetime/tests.rs b/src/datetime/tests.rs index 3b46f3f1bf..fbf959e10d 100644 --- a/src/datetime/tests.rs +++ b/src/datetime/tests.rs @@ -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)) diff --git a/src/format/parsed.rs b/src/format/parsed.rs index 011ad889aa..4665fcaf7c 100644 --- a/src/format/parsed.rs +++ b/src/format/parsed.rs @@ -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),