1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

sleep: use Duration::saturating_add to sum times

Use `Duration::saturating_add()` to avoid a panic due to overflow when
adding the durations provided as command-line arguments.
This commit is contained in:
Jeffrey Finkelstein 2022-03-18 22:31:02 -04:00
parent 388cb6c83a
commit ce2a026ff8
2 changed files with 11 additions and 1 deletions

View file

@ -63,7 +63,7 @@ fn sleep(args: &[&str]) -> UResult<()> {
args.iter().try_fold(
Duration::new(0, 0),
|result, arg| match uucore::parse_time::from_str(&arg[..]) {
Ok(m) => Ok(m + result),
Ok(m) => Ok(m.saturating_add(result)),
Err(f) => Err(USimpleError::new(1, f)),
},
)?;

View file

@ -131,3 +131,13 @@ fn test_dont_overflow() {
.no_stderr()
.no_stdout();
}
// #[test]
#[allow(dead_code)]
fn test_sum_overflow() {
new_ucmd!()
.args(&["100000000000000d", "100000000000000d", "100000000000000d"])
.succeeds()
.no_stderr()
.no_stdout();
}