mirror of
https://github.com/uutils/coreutils.git
synced 2026-06-10 15:48:22 -07: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:
@@ -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)),
|
||||
},
|
||||
)?;
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user