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:
parent
388cb6c83a
commit
ce2a026ff8
2 changed files with 11 additions and 1 deletions
|
@ -63,7 +63,7 @@ fn sleep(args: &[&str]) -> UResult<()> {
|
||||||
args.iter().try_fold(
|
args.iter().try_fold(
|
||||||
Duration::new(0, 0),
|
Duration::new(0, 0),
|
||||||
|result, arg| match uucore::parse_time::from_str(&arg[..]) {
|
|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)),
|
Err(f) => Err(USimpleError::new(1, f)),
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
|
|
|
@ -131,3 +131,13 @@ fn test_dont_overflow() {
|
||||||
.no_stderr()
|
.no_stderr()
|
||||||
.no_stdout();
|
.no_stdout();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// #[test]
|
||||||
|
#[allow(dead_code)]
|
||||||
|
fn test_sum_overflow() {
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["100000000000000d", "100000000000000d", "100000000000000d"])
|
||||||
|
.succeeds()
|
||||||
|
.no_stderr()
|
||||||
|
.no_stdout();
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue