From bcfffd20ab9db3906135aec938054d57a603f30d Mon Sep 17 00:00:00 2001 From: Arpit Jain <3242828+arpitjain099@users.noreply.github.com> Date: Mon, 9 Nov 2026 16:34:48 +0000 Subject: [PATCH] fix(time): avoid i32 overflow when converting an out-of-range date (#10299) Signed-off-by: Arpit Jain Co-authored-by: Claude (cherry picked from commit 402e4a97fc0b7c082cc419b380c6014388dd1a10) Signed-off-by: WenyXu --- src/common/time/src/date.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/common/time/src/date.rs b/src/common/time/src/date.rs index 38667abbcac..f474c372d19 100644 --- a/src/common/time/src/date.rs +++ b/src/common/time/src/date.rs @@ -55,7 +55,7 @@ impl From for Date { impl Display for Date { /// [Date] is formatted according to ISO-8601 standard. fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - if let Some(abs_date) = NaiveDate::from_num_days_from_ce_opt(UNIX_EPOCH_FROM_CE + self.0) { + if let Some(abs_date) = self.to_chrono_date() { write!(f, "{}", abs_date.format("%F")) } else { write!(f, "Date({})", self.0) @@ -93,7 +93,9 @@ impl Date { } pub fn to_chrono_date(&self) -> Option { - NaiveDate::from_num_days_from_ce_opt(UNIX_EPOCH_FROM_CE + self.0) + UNIX_EPOCH_FROM_CE + .checked_add(self.0) + .and_then(NaiveDate::from_num_days_from_ce_opt) } /// Format Date for given format and timezone. @@ -217,6 +219,12 @@ mod tests { assert_eq!("1970-02-12", Date::new(42).to_string()); } + #[test] + fn test_to_chrono_date_out_of_range() { + assert_eq!(None, Date::new(i32::MAX).to_chrono_date()); + assert_eq!("Date(2147483647)", Date::new(i32::MAX).to_string()); + } + #[test] pub fn test_date_parse() { assert_eq!(