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

Merge pull request #2328 from miDeb/seq/validator

seq: reject NaN arguments
This commit is contained in:
Sylvestre Ledru 2021-06-02 11:20:13 +02:00 committed by GitHub
commit 132ddf98b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 27 deletions

View file

@ -62,7 +62,6 @@ impl Number {
impl FromStr for Number { impl FromStr for Number {
type Err = String; type Err = String;
/// Tries to parse this string as a BigInt, or if that fails as an f64.
fn from_str(mut s: &str) -> Result<Self, Self::Err> { fn from_str(mut s: &str) -> Result<Self, Self::Err> {
if s.starts_with('+') { if s.starts_with('+') {
s = &s[1..]; s = &s[1..];
@ -71,10 +70,16 @@ impl FromStr for Number {
match s.parse::<BigInt>() { match s.parse::<BigInt>() {
Ok(n) => Ok(Number::BigInt(n)), Ok(n) => Ok(Number::BigInt(n)),
Err(_) => match s.parse::<f64>() { Err(_) => match s.parse::<f64>() {
Ok(n) => Ok(Number::F64(n)), Ok(value) if value.is_nan() => Err(format!(
Err(e) => Err(format!( "invalid 'not-a-number' argument: '{}'\nTry '{} --help' for more information.",
"seq: invalid floating point argument `{}`: {}", s,
s, e executable!(),
)),
Ok(value) => Ok(Number::F64(value)),
Err(_) => Err(format!(
"invalid floating point argument: '{}'\nTry '{} --help' for more information.",
s,
executable!(),
)), )),
}, },
} }
@ -136,13 +141,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
let dec = slice.find('.').unwrap_or(len); let dec = slice.find('.').unwrap_or(len);
largest_dec = len - dec; largest_dec = len - dec;
padding = dec; padding = dec;
match slice.parse() { return_if_err!(1, slice.parse())
Ok(n) => n,
Err(s) => {
show_error!("{}", s);
return 1;
}
}
} else { } else {
Number::BigInt(BigInt::one()) Number::BigInt(BigInt::one())
}; };
@ -152,30 +151,22 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
let dec = slice.find('.').unwrap_or(len); let dec = slice.find('.').unwrap_or(len);
largest_dec = cmp::max(largest_dec, len - dec); largest_dec = cmp::max(largest_dec, len - dec);
padding = cmp::max(padding, dec); padding = cmp::max(padding, dec);
match slice.parse() { return_if_err!(1, slice.parse())
Ok(n) => n,
Err(s) => {
show_error!("{}", s);
return 1;
}
}
} else { } else {
Number::BigInt(BigInt::one()) Number::BigInt(BigInt::one())
}; };
if increment.is_zero() { if increment.is_zero() {
show_error!("increment value: '{}'", numbers[1]); show_error!(
"invalid Zero increment value: '{}'\nTry '{} --help' for more information.",
numbers[1],
executable!()
);
return 1; return 1;
} }
let last = { let last = {
let slice = numbers[numbers.len() - 1]; let slice = numbers[numbers.len() - 1];
padding = cmp::max(padding, slice.find('.').unwrap_or_else(|| slice.len())); padding = cmp::max(padding, slice.find('.').unwrap_or_else(|| slice.len()));
match slice.parse::<Number>() { return_if_err!(1, slice.parse())
Ok(n) => n,
Err(s) => {
show_error!("{}", s);
return 1;
}
}
}; };
if largest_dec > 0 { if largest_dec > 0 {
largest_dec -= 1; largest_dec -= 1;

View file

@ -1,5 +1,21 @@
use crate::common::util::*; use crate::common::util::*;
#[test]
fn test_rejects_nan() {
new_ucmd!()
.args(&["NaN"])
.fails()
.stderr_only("seq: invalid 'not-a-number' argument: 'NaN'\nTry 'seq --help' for more information.");
}
#[test]
fn test_rejects_non_floats() {
new_ucmd!()
.args(&["foo"])
.fails()
.stderr_only("seq: invalid floating point argument: 'foo'\nTry 'seq --help' for more information.");
}
// ---- Tests for the big integer based path ---- // ---- Tests for the big integer based path ----
#[test] #[test]