From bf456f466fa26f62459a5abdb454f64706a4657d Mon Sep 17 00:00:00 2001 From: Chris Gillespie <6572184+gillespiecd@users.noreply.github.com> Date: Sun, 4 Oct 2020 16:17:44 -0700 Subject: [PATCH] Handle panic from large integers --- src/lib.rs | 8 ++++---- src/tests/fuzzing.rs | 12 ++++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e9a3de5..5b9c74f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -951,8 +951,7 @@ impl Parser { let m = res.month.unwrap_or_else(|| default.month() as i32) as u32; let d_offset = if res.weekday.is_some() && res.day.is_none() { - // TODO: Unwrap not justified - let dow = day_of_week(y as u32, m, default.day()).unwrap(); + let dow = day_of_week(y as u32, m, default.day())?; // UNWRAP: We've already check res.weekday() is some let actual_weekday = (res.weekday.unwrap() + 1) % 7; @@ -963,14 +962,15 @@ impl Parser { }; // TODO: Change month/day to u32 - let d = NaiveDate::from_ymd( + let d = NaiveDate::from_ymd_opt( y, m, min( res.day.unwrap_or(default.day() as i32) as u32, days_in_month(y, m as i32)?, ), - ); + ) + .ok_or_else(|| ParseError::ImpossibleTimestamp("Invalid date range given"))?; let d = d + d_offset; diff --git a/src/tests/fuzzing.rs b/src/tests/fuzzing.rs index 4fd99d5..f7c60ef 100644 --- a/src/tests/fuzzing.rs +++ b/src/tests/fuzzing.rs @@ -44,6 +44,18 @@ fn large_int() { assert!(parse_result.is_err()); } +#[test] +fn another_large_int() { + let parse_result = parse("1412409095009"); + assert!(parse_result.is_err()); +} + +#[test] +fn an_even_larger_int() { + let parse_result = parse("1566997680962280"); + assert!(parse_result.is_err()); +} + #[test] fn empty_string() { assert_eq!(parse(""), Err(ParseError::NoDate))