Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle panic while converting to Duration #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use chrono::{Duration, Local, NaiveDate, Utc};
use regex::{Error as RegexError, Regex};
use std::error::Error;
use std::fmt::{self, Display};
use std::panic;

#[derive(Debug, PartialEq)]
pub enum ParseDurationError {
Expand Down Expand Up @@ -157,7 +158,7 @@ pub fn from_str_at_date(date: NaiveDate, s: &str) -> Result<Duration, ParseDurat
is_ago = true;
}

let duration = match unit {
let duration = panic::catch_unwind(|| match unit {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is too broad

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably second only

Copy link
Contributor Author

@undali undali Jun 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, did you mean to catch panic only for case Duration::seconds ?
But it seems 8 out of the 11 case can cause panic when called with very high value.

One more thing, Should I remove the test case? It causes test failure in Basic:CI probably because of the option RUSTDOCFLAGS: -Cpanic=abort.

Thanks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@undali sorry but thinking more about this, it should probably done at the chrono level directly.
They should probably not panic when hitting this error

"years" | "year" => Duration::days(value * 365),
"months" | "month" => Duration::days(value * 30),
"fortnights" | "fortnight" => Duration::weeks(value * 2),
Expand All @@ -169,8 +170,10 @@ pub fn from_str_at_date(date: NaiveDate, s: &str) -> Result<Duration, ParseDurat
"yesterday" => Duration::days(-1),
"tomorrow" => Duration::days(1),
"now" | "today" => Duration::zero(),
_ => return Err(ParseDurationError::InvalidInput),
};
_ => panic!("Invalid Input"),
})
.or(Err(ParseDurationError::InvalidInput))?;

let neg_duration = -duration;
total_duration =
match total_duration.checked_add(if is_ago { &neg_duration } else { &duration }) {
Expand Down
6 changes: 6 additions & 0 deletions tests/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,9 @@ fn test_invalid_input_at_date() {
let result = from_str_at_date(today, "invalid 1r");
assert_eq!(result, Err(ParseDurationError::InvalidInput));
}

#[test]
fn test_large_input() {
let result = humantime_to_duration::from_str("8888888888888h");
assert_eq!(result, Err(ParseDurationError::InvalidInput));
}