mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-27 19:17:43 +00:00
date: fix panic when input will cause overflow (#5160)
* fix issue 5149 * fix clippy style issue * fix spell issue * address comment * address comments * fix cspell
This commit is contained in:
parent
abffe6c768
commit
b5746f794c
2 changed files with 23 additions and 2 deletions
|
@ -227,8 +227,20 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||||
DateSource::Human(relative_time) => {
|
DateSource::Human(relative_time) => {
|
||||||
// Get the current DateTime<FixedOffset> for things like "1 year ago"
|
// Get the current DateTime<FixedOffset> for things like "1 year ago"
|
||||||
let current_time = DateTime::<FixedOffset>::from(Local::now());
|
let current_time = DateTime::<FixedOffset>::from(Local::now());
|
||||||
let iter = std::iter::once(Ok(current_time + relative_time));
|
// double check the result is overflow or not of the current_time + relative_time
|
||||||
Box::new(iter)
|
// 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) => {
|
DateSource::File(ref path) => {
|
||||||
if path.is_dir() {
|
if path.is_dir() {
|
||||||
|
|
|
@ -395,3 +395,12 @@ fn test_invalid_date_string() {
|
||||||
.no_stdout()
|
.no_stdout()
|
||||||
.stderr_contains("invalid date");
|
.stderr_contains("invalid date");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_date_overflow() {
|
||||||
|
new_ucmd!()
|
||||||
|
.arg("-d68888888888888sms")
|
||||||
|
.fails()
|
||||||
|
.no_stdout()
|
||||||
|
.stderr_contains("invalid date");
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue