From 0b06f2b4a8543a014afa15927728d16d9ba38eca Mon Sep 17 00:00:00 2001 From: Nikolai Date: Wed, 19 Apr 2023 05:06:32 +0100 Subject: [PATCH 1/4] fixing seq panic on none --- src/uu/seq/src/error.rs | 24 +++++++++++++++++++----- src/uu/seq/src/seq.rs | 11 +++++++---- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/uu/seq/src/error.rs b/src/uu/seq/src/error.rs index ae641a978..ad6b5733a 100644 --- a/src/uu/seq/src/error.rs +++ b/src/uu/seq/src/error.rs @@ -25,6 +25,9 @@ pub enum SeqError { /// The parameter is the increment argument as a [`String`] as read /// from the command line. ZeroIncrement(String), + + /// No arguments were passed to this function, 1 or more is required + NoArguments, } impl SeqError { @@ -33,6 +36,7 @@ impl SeqError { match self { Self::ParseError(s, _) => s, Self::ZeroIncrement(s) => s, + Self::NoArguments => "", } } @@ -40,11 +44,12 @@ impl SeqError { fn argtype(&self) -> &str { match self { Self::ParseError(_, e) => match e { - ParseNumberError::Float => "floating point argument", - ParseNumberError::Nan => "'not-a-number' argument", - ParseNumberError::Hex => "hexadecimal argument", + ParseNumberError::Float => "invalid floating point argument: ", + ParseNumberError::Nan => "invalid 'not-a-number' argument: ", + ParseNumberError::Hex => "invalid hexadecimal argument: ", }, - Self::ZeroIncrement(_) => "Zero increment value", + Self::ZeroIncrement(_) => "invalid Zero increment value: ", + Self::NoArguments => "missing operand", } } } @@ -63,6 +68,15 @@ impl Error for SeqError {} impl Display for SeqError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "invalid {}: {}", self.argtype(), self.arg().quote()) + write!( + f, + "{}{}", + self.argtype(), + if self.arg() != "" { + self.arg().quote().to_string() + } else { + "".to_string() + } + ) } } diff --git a/src/uu/seq/src/seq.rs b/src/uu/seq/src/seq.rs index 97382ed1b..44e0a7744 100644 --- a/src/uu/seq/src/seq.rs +++ b/src/uu/seq/src/seq.rs @@ -58,10 +58,13 @@ type RangeFloat = (ExtendedBigDecimal, ExtendedBigDecimal, ExtendedBigDecimal); pub fn uumain(args: impl uucore::Args) -> UResult<()> { let matches = uu_app().try_get_matches_from(args)?; - let numbers = matches - .get_many::(ARG_NUMBERS) - .unwrap() - .collect::>(); + let numbers_option = matches.get_many::(ARG_NUMBERS); + + if numbers_option.is_none() { + return Err(SeqError::NoArguments.into()); + } + + let numbers = numbers_option.unwrap().collect::>(); let options = SeqOptions { separator: matches From 644fd1c93ed38a0752380f111c93b9c7b6ff6256 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sun, 25 Jun 2023 22:29:08 +0200 Subject: [PATCH 2/4] fix some clippy warnings --- src/uu/seq/src/error.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/uu/seq/src/error.rs b/src/uu/seq/src/error.rs index ad6b5733a..4945914d1 100644 --- a/src/uu/seq/src/error.rs +++ b/src/uu/seq/src/error.rs @@ -72,10 +72,10 @@ impl Display for SeqError { f, "{}{}", self.argtype(), - if self.arg() != "" { - self.arg().quote().to_string() + if self.arg() == "" { + String::new() } else { - "".to_string() + self.arg().quote().to_string() } ) } From 98264e9cdf62cc891c5cfeaac8f20c8f3a04848e Mon Sep 17 00:00:00 2001 From: Daniel Hofstetter Date: Wed, 5 Jul 2023 15:51:23 +0200 Subject: [PATCH 3/4] seq: add test for call without args --- tests/by-util/test_seq.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/by-util/test_seq.rs b/tests/by-util/test_seq.rs index 63015b24a..187e7533f 100644 --- a/tests/by-util/test_seq.rs +++ b/tests/by-util/test_seq.rs @@ -7,6 +7,14 @@ fn test_invalid_arg() { new_ucmd!().arg("--definitely-invalid").fails().code_is(1); } +#[test] +fn test_no_args() { + new_ucmd!() + .fails() + .code_is(1) + .stderr_contains("missing operand"); +} + #[test] fn test_hex_rejects_sign_after_identifier() { new_ucmd!() From ac4ff2ac0e3056550c292a5e862eb3e1aaa2480a Mon Sep 17 00:00:00 2001 From: Daniel Hofstetter Date: Thu, 6 Jul 2023 10:26:29 +0200 Subject: [PATCH 4/4] seq: simplify error handling Co-authored-by: Terts Diepraam --- src/uu/seq/src/error.rs | 45 +++++++++++------------------------------ 1 file changed, 12 insertions(+), 33 deletions(-) diff --git a/src/uu/seq/src/error.rs b/src/uu/seq/src/error.rs index 4945914d1..fc8452e13 100644 --- a/src/uu/seq/src/error.rs +++ b/src/uu/seq/src/error.rs @@ -2,7 +2,7 @@ // * // * For the full copyright and license information, please view the LICENSE // * file that was distributed with this source code. -// spell-checker:ignore numberparse argtype +// spell-checker:ignore numberparse //! Errors returned by seq. use std::error::Error; use std::fmt::Display; @@ -30,29 +30,6 @@ pub enum SeqError { NoArguments, } -impl SeqError { - /// The [`String`] argument as read from the command-line. - fn arg(&self) -> &str { - match self { - Self::ParseError(s, _) => s, - Self::ZeroIncrement(s) => s, - Self::NoArguments => "", - } - } - - /// The type of argument that is causing the error. - fn argtype(&self) -> &str { - match self { - Self::ParseError(_, e) => match e { - ParseNumberError::Float => "invalid floating point argument: ", - ParseNumberError::Nan => "invalid 'not-a-number' argument: ", - ParseNumberError::Hex => "invalid hexadecimal argument: ", - }, - Self::ZeroIncrement(_) => "invalid Zero increment value: ", - Self::NoArguments => "missing operand", - } - } -} impl UError for SeqError { /// Always return 1. fn code(&self) -> i32 { @@ -68,15 +45,17 @@ impl Error for SeqError {} impl Display for SeqError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!( - f, - "{}{}", - self.argtype(), - if self.arg() == "" { - String::new() - } else { - self.arg().quote().to_string() + match self { + Self::ParseError(s, e) => { + let error_type = match e { + ParseNumberError::Float => "floating point", + ParseNumberError::Nan => "'not-a-number'", + ParseNumberError::Hex => "hexadecimal", + }; + write!(f, "invalid {error_type} argument: {}", s.quote()) } - ) + Self::ZeroIncrement(s) => write!(f, "invalid Zero increment value: {}", s.quote()), + Self::NoArguments => write!(f, "missing operand"), + } } }