mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
uucore: error on negative interval in parse_time
Return an error when a negative interval is provided as the argument to `uucore::parse_time::from_str()`, since a `Duration` should only be non-negative.
This commit is contained in:
parent
5eeac5881a
commit
f4af226820
3 changed files with 25 additions and 0 deletions
|
@ -63,6 +63,10 @@ pub fn from_str(string: &str) -> Result<Duration, String> {
|
||||||
.parse::<f64>()
|
.parse::<f64>()
|
||||||
.map_err(|e| format!("invalid time interval {}: {}", string.quote(), e))?;
|
.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;
|
const NANOS_PER_SEC: u32 = 1_000_000_000;
|
||||||
let whole_secs = num.trunc();
|
let whole_secs = num.trunc();
|
||||||
let nanos = (num.fract() * (NANOS_PER_SEC as f64)).trunc();
|
let nanos = (num.fract() * (NANOS_PER_SEC as f64)).trunc();
|
||||||
|
@ -105,4 +109,9 @@ mod tests {
|
||||||
fn test_error_invalid_magnitude() {
|
fn test_error_invalid_magnitude() {
|
||||||
assert!(from_str("12abc3s").is_err());
|
assert!(from_str("12abc3s").is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_negative() {
|
||||||
|
assert!(from_str("-1").is_err());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -149,3 +149,11 @@ fn test_sum_overflow() {
|
||||||
.no_stderr()
|
.no_stderr()
|
||||||
.no_stdout();
|
.no_stdout();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_negative_interval() {
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["--", "-1"])
|
||||||
|
.fails()
|
||||||
|
.usage_error("invalid time interval '-1'");
|
||||||
|
}
|
||||||
|
|
|
@ -89,3 +89,11 @@ fn test_dont_overflow() {
|
||||||
.no_stderr()
|
.no_stderr()
|
||||||
.no_stdout();
|
.no_stdout();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_negative_interval() {
|
||||||
|
new_ucmd!()
|
||||||
|
.args(&["--", "-1", "sleep", "0"])
|
||||||
|
.fails()
|
||||||
|
.usage_error("invalid time interval '-1'");
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue