From 4f33a375cda8c515db6eb81cd54007fe850dd2cb Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Fri, 5 Jan 2024 20:55:54 +0100 Subject: [PATCH 1/4] factor: handle the '< .' arg --- src/uu/factor/src/cli.rs | 15 ++++++++++++--- tests/by-util/test_factor.rs | 5 +++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/uu/factor/src/cli.rs b/src/uu/factor/src/cli.rs index 63a0632a3..d01ca625c 100644 --- a/src/uu/factor/src/cli.rs +++ b/src/uu/factor/src/cli.rs @@ -73,9 +73,18 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let stdin = stdin(); let lines = stdin.lock().lines(); for line in lines { - for number in line.unwrap().split_whitespace() { - print_factors_str(number, &mut w, print_exponents) - .map_err_context(|| "write error".into())?; + match line { + Ok(line) => { + for number in line.split_whitespace() { + print_factors_str(number, &mut w, print_exponents) + .map_err_context(|| "write error".into())?; + } + } + Err(e) => { + set_exit_code(1); + show_error!("error reading input: {}", e); + return Ok(()); + } } } } diff --git a/tests/by-util/test_factor.rs b/tests/by-util/test_factor.rs index 3326a1ace..57a2dae09 100644 --- a/tests/by-util/test_factor.rs +++ b/tests/by-util/test_factor.rs @@ -1258,3 +1258,8 @@ const PRIMES50: &[u64] = &[ 1125899906841623, 1125899906841613, ]; + +#[test] +fn fails_on_directory() { + new_ucmd!().pipe_in(".").fails(); +} From 8d24036f5cf769f6a10672e6060c615599269f2a Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Fri, 5 Jan 2024 21:15:26 +0100 Subject: [PATCH 2/4] basenc: handle '--base32 .' arg --- src/uu/base32/src/base_common.rs | 3 ++- src/uucore/src/lib/features/encoding.rs | 7 +++++-- tests/by-util/test_basenc.rs | 13 +++++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/uu/base32/src/base_common.rs b/src/uu/base32/src/base_common.rs index 2112a40ea..68c40287d 100644 --- a/src/uu/base32/src/base_common.rs +++ b/src/uu/base32/src/base_common.rs @@ -6,7 +6,7 @@ use std::io::{stdout, Read, Write}; use uucore::display::Quotable; -use uucore::encoding::{wrap_print, Data, Format}; +use uucore::encoding::{wrap_print, Data, EncodeError, Format}; use uucore::error::{FromIo, UResult, USimpleError, UUsageError}; use uucore::format_usage; @@ -174,6 +174,7 @@ pub fn handle_input( wrap_print(&data, &s); Ok(()) } + Err(EncodeError::InvalidInput) => Err(USimpleError::new(1, "error: invalid input")), Err(_) => Err(USimpleError::new( 1, "error: invalid input (length must be multiple of 4 characters)", diff --git a/src/uucore/src/lib/features/encoding.rs b/src/uucore/src/lib/features/encoding.rs index 14fdbb38e..db218d5f0 100644 --- a/src/uucore/src/lib/features/encoding.rs +++ b/src/uucore/src/lib/features/encoding.rs @@ -27,6 +27,7 @@ pub enum DecodeError { pub enum EncodeError { Z85InputLenNotMultipleOf4, + InvalidInput, } pub type DecodeResult = Result, DecodeError>; @@ -148,8 +149,10 @@ impl Data { pub fn encode(&mut self) -> Result { let mut buf: Vec = vec![]; - self.input.read_to_end(&mut buf).unwrap(); - encode(self.format, buf.as_slice()) + match self.input.read_to_end(&mut buf) { + Ok(_) => encode(self.format, buf.as_slice()), + Err(_) => Err(EncodeError::InvalidInput), + } } } diff --git a/tests/by-util/test_basenc.rs b/tests/by-util/test_basenc.rs index 6c71b63f7..13a896703 100644 --- a/tests/by-util/test_basenc.rs +++ b/tests/by-util/test_basenc.rs @@ -18,3 +18,16 @@ fn test_z85_not_padded() { .fails() .stderr_only("basenc: error: invalid input (length must be multiple of 4 characters)\n"); } + +#[test] +fn test_invalid_input() { + let error_message = if cfg!(windows) { + "basenc: .: Permission denied" + } else { + "basenc: error: invalid input\n" + }; + new_ucmd!() + .args(&["--base32", "."]) + .fails() + .stderr_only(error_message); +} From 3d356d47b3f6ada89b4946983d904f578f5263e2 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Fri, 5 Jan 2024 21:44:58 +0100 Subject: [PATCH 3/4] expand: avoid an infinite loop --- src/uu/expand/src/expand.rs | 11 +++++++++-- tests/by-util/test_expand.rs | 8 ++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/uu/expand/src/expand.rs b/src/uu/expand/src/expand.rs index eb9766eb4..1efb36c65 100644 --- a/src/uu/expand/src/expand.rs +++ b/src/uu/expand/src/expand.rs @@ -11,11 +11,12 @@ use std::fmt; use std::fs::File; use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Read, Write}; use std::num::IntErrorKind; +use std::path::Path; use std::str::from_utf8; use unicode_width::UnicodeWidthChar; use uucore::display::Quotable; -use uucore::error::{FromIo, UError, UResult}; -use uucore::{format_usage, help_about, help_usage}; +use uucore::error::{set_exit_code, FromIo, UError, UResult}; +use uucore::{format_usage, help_about, help_usage, show_error}; const ABOUT: &str = help_about!("expand.md"); const USAGE: &str = help_usage!("expand.md"); @@ -465,6 +466,12 @@ fn expand(options: &Options) -> UResult<()> { let mut buf = Vec::new(); for file in &options.files { + if Path::new(file).is_dir() { + show_error!("{}: Is a directory", file); + set_exit_code(1); + continue; + } + let mut fh = open(file)?; while match fh.read_until(b'\n', &mut buf) { diff --git a/tests/by-util/test_expand.rs b/tests/by-util/test_expand.rs index 1e26b3273..c420f5ad5 100644 --- a/tests/by-util/test_expand.rs +++ b/tests/by-util/test_expand.rs @@ -409,3 +409,11 @@ int main() { ", ); } + +#[test] +fn test_expand_directory() { + new_ucmd!() + .args(&["."]) + .fails() + .stderr_contains("expand: .: Is a directory"); +} From b116a97fdcd8533f296f0c2e0f1a94e3b287d0e5 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Mon, 15 Jan 2024 10:43:20 +0100 Subject: [PATCH 4/4] add missing \n --- tests/by-util/test_basenc.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/by-util/test_basenc.rs b/tests/by-util/test_basenc.rs index 13a896703..c9e15ef1f 100644 --- a/tests/by-util/test_basenc.rs +++ b/tests/by-util/test_basenc.rs @@ -22,7 +22,7 @@ fn test_z85_not_padded() { #[test] fn test_invalid_input() { let error_message = if cfg!(windows) { - "basenc: .: Permission denied" + "basenc: .: Permission denied\n" } else { "basenc: error: invalid input\n" };