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

date: fix panic when input will cause overflow #5160

Merged
merged 8 commits into from
Aug 20, 2023
Merged
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
16 changes: 14 additions & 2 deletions src/uu/date/src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,20 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
DateSource::Human(relative_time) => {
// Get the current DateTime<FixedOffset> for things like "1 year ago"
let current_time = DateTime::<FixedOffset>::from(Local::now());
let iter = std::iter::once(Ok(current_time + relative_time));
Box::new(iter)
// double check the result is overflow or not of the current_time + relative_time
// it may cause a panic of chrono::datetime::DateTime add
match current_time.checked_add_signed(relative_time) {
Some(date) => {
let iter = std::iter::once(Ok(date));
Box::new(iter)
}
None => {
return Err(USimpleError::new(
1,
format!("invalid date {}", relative_time),
));
}
}
}
DateSource::File(ref path) => {
if path.is_dir() {
Expand Down
9 changes: 9 additions & 0 deletions tests/by-util/test_date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,12 @@ fn test_invalid_date_string() {
.no_stdout()
.stderr_contains("invalid date");
}

#[test]
fn test_date_overflow() {
new_ucmd!()
.arg("-d68888888888888sms")
.fails()
.no_stdout()
.stderr_contains("invalid date");
}