1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-09-14 19:16:17 +00:00

Merge pull request #3292 from jfinkels/uucore-parse-time-negative-interval

uucore: error on negative interval in parse_time::from_str()
This commit is contained in:
Sylvestre Ledru 2022-03-22 08:07:58 +01:00 committed by GitHub
commit b50d96e193
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 0 deletions

View file

@ -63,6 +63,10 @@ pub fn from_str(string: &str) -> Result<Duration, String> {
.parse::<f64>()
.map_err(|e| format!("invalid time interval {}: {}", string.quote(), e))?;
if num < 0. {
return Err(format!("invalid time interval {}", string.quote()));
}
const NANOS_PER_SEC: u32 = 1_000_000_000;
let whole_secs = num.trunc();
let nanos = (num.fract() * (NANOS_PER_SEC as f64)).trunc();
@ -105,4 +109,9 @@ mod tests {
fn test_error_invalid_magnitude() {
assert!(from_str("12abc3s").is_err());
}
#[test]
fn test_negative() {
assert!(from_str("-1").is_err());
}
}

View file

@ -149,3 +149,11 @@ fn test_sum_overflow() {
.no_stderr()
.no_stdout();
}
#[test]
fn test_negative_interval() {
new_ucmd!()
.args(&["--", "-1"])
.fails()
.usage_error("invalid time interval '-1'");
}

View file

@ -89,3 +89,11 @@ fn test_dont_overflow() {
.no_stderr()
.no_stdout();
}
#[test]
fn test_negative_interval() {
new_ucmd!()
.args(&["--", "-1", "sleep", "0"])
.fails()
.usage_error("invalid time interval '-1'");
}