Skip to content

Commit 096b1e3

Browse files
committed
fix: check overflows during date time composition
1 parent f1f2c47 commit 096b1e3

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

src/items/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ fn at_date_inner(date: Vec<Item>, at: DateTime<FixedOffset>) -> Option<DateTime<
356356
let delta = (day.num_days_from_monday() as i32
357357
- d.weekday().num_days_from_monday() as i32)
358358
.rem_euclid(7)
359-
+ x * 7;
359+
+ x.checked_mul(7)?;
360360

361361
d = if delta < 0 {
362362
d.checked_sub_days(chrono::Days::new((-delta) as u64))?
@@ -394,11 +394,11 @@ fn at_date_inner(date: Vec<Item>, at: DateTime<FixedOffset>) -> Option<DateTime<
394394
relative::Relative::Days(x) => d += chrono::Duration::days(x.into()),
395395
relative::Relative::Hours(x) => d += chrono::Duration::hours(x.into()),
396396
relative::Relative::Minutes(x) => {
397-
d += chrono::Duration::minutes(x.into());
397+
d += chrono::Duration::try_minutes(x.into())?;
398398
}
399399
// Seconds are special because they can be given as a float
400400
relative::Relative::Seconds(x) => {
401-
d += chrono::Duration::seconds(x as i64);
401+
d += chrono::Duration::try_seconds(x as i64)?;
402402
}
403403
}
404404
}

src/items/relative.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@ pub enum Relative {
5454
}
5555

5656
impl Relative {
57+
// TODO: determine how to handle multiplication overflows,
58+
// using saturating_mul for now.
5759
fn mul(self, n: i32) -> Self {
5860
match self {
59-
Self::Years(x) => Self::Years(n * x),
60-
Self::Months(x) => Self::Months(n * x),
61-
Self::Days(x) => Self::Days(n * x),
62-
Self::Hours(x) => Self::Hours(n * x),
63-
Self::Minutes(x) => Self::Minutes(n * x),
61+
Self::Years(x) => Self::Years(n.saturating_mul(x)),
62+
Self::Months(x) => Self::Months(n.saturating_mul(x)),
63+
Self::Days(x) => Self::Days(n.saturating_mul(x)),
64+
Self::Hours(x) => Self::Hours(n.saturating_mul(x)),
65+
Self::Minutes(x) => Self::Minutes(n.saturating_mul(x)),
6466
Self::Seconds(x) => Self::Seconds(f64::from(n) * x),
6567
}
6668
}

0 commit comments

Comments
 (0)