mirror of
https://github.com/bspeice/dtparse
synced 2024-12-22 04:18:09 -05:00
Handle panic from large integers
This commit is contained in:
parent
6a5ec31d8e
commit
bf456f466f
@ -951,8 +951,7 @@ impl Parser {
|
|||||||
let m = res.month.unwrap_or_else(|| default.month() as i32) as u32;
|
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() {
|
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())?;
|
||||||
let dow = day_of_week(y as u32, m, default.day()).unwrap();
|
|
||||||
|
|
||||||
// UNWRAP: We've already check res.weekday() is some
|
// UNWRAP: We've already check res.weekday() is some
|
||||||
let actual_weekday = (res.weekday.unwrap() + 1) % 7;
|
let actual_weekday = (res.weekday.unwrap() + 1) % 7;
|
||||||
@ -963,14 +962,15 @@ impl Parser {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// TODO: Change month/day to u32
|
// TODO: Change month/day to u32
|
||||||
let d = NaiveDate::from_ymd(
|
let d = NaiveDate::from_ymd_opt(
|
||||||
y,
|
y,
|
||||||
m,
|
m,
|
||||||
min(
|
min(
|
||||||
res.day.unwrap_or(default.day() as i32) as u32,
|
res.day.unwrap_or(default.day() as i32) as u32,
|
||||||
days_in_month(y, m as i32)?,
|
days_in_month(y, m as i32)?,
|
||||||
),
|
),
|
||||||
);
|
)
|
||||||
|
.ok_or_else(|| ParseError::ImpossibleTimestamp("Invalid date range given"))?;
|
||||||
|
|
||||||
let d = d + d_offset;
|
let d = d + d_offset;
|
||||||
|
|
||||||
|
@ -44,6 +44,18 @@ fn large_int() {
|
|||||||
assert!(parse_result.is_err());
|
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]
|
#[test]
|
||||||
fn empty_string() {
|
fn empty_string() {
|
||||||
assert_eq!(parse(""), Err(ParseError::NoDate))
|
assert_eq!(parse(""), Err(ParseError::NoDate))
|
||||||
|
Loading…
Reference in New Issue
Block a user