Skip to content

Commit f659719

Browse files
pitdickerdjc
authored andcommitted
Fix panic in from_num_days_from_ce_opt
1 parent a20a9b7 commit f659719

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/naive/date.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ impl NaiveDate {
448448
/// ```
449449
#[must_use]
450450
pub fn from_num_days_from_ce_opt(days: i32) -> Option<NaiveDate> {
451-
let days = days + 365; // make December 31, 1 BCE equal to day 0
451+
let days = days.checked_add(365)?; // make December 31, 1 BCE equal to day 0
452452
let (year_div_400, cycle) = div_mod_floor(days, 146_097);
453453
let (year_mod_400, ordinal) = internals::cycle_to_yo(cycle as u32);
454454
let flags = YearFlags::from_year_mod_400(year_mod_400 as i32);
@@ -2500,6 +2500,9 @@ mod tests {
25002500
assert_eq!(from_ndays_from_ce(NaiveDate::MIN.num_days_from_ce() - 1), None);
25012501
assert_eq!(from_ndays_from_ce(NaiveDate::MAX.num_days_from_ce()), Some(NaiveDate::MAX));
25022502
assert_eq!(from_ndays_from_ce(NaiveDate::MAX.num_days_from_ce() + 1), None);
2503+
2504+
assert_eq!(from_ndays_from_ce(i32::MIN), None);
2505+
assert_eq!(from_ndays_from_ce(i32::MAX), None);
25032506
}
25042507

25052508
#[test]

0 commit comments

Comments
 (0)