diff --git a/src/uu/fold/src/fold.rs b/src/uu/fold/src/fold.rs index ecd08264f..97feeeea5 100644 --- a/src/uu/fold/src/fold.rs +++ b/src/uu/fold/src/fold.rs @@ -64,12 +64,21 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { }; let width = match poss_width { - Some(inp_width) => inp_width.parse::().map_err(|e| { - USimpleError::new( - 1, - translate!("fold-error-illegal-width", "width" => inp_width.quote(), "error" => e), - ) - })?, + Some(inp_width) => match inp_width.parse::() { + Ok(0) => { + return Err(USimpleError::new( + 1, + translate!("fold-error-illegal-width", "width" => inp_width.quote()), + )); + } + Ok(parsed_width) => parsed_width, + Err(e) => { + return Err(USimpleError::new( + 1, + translate!("fold-error-illegal-width", "width" => inp_width.quote(), "error" => e), + )); + } + }, None => 80, }; diff --git a/tests/by-util/test_fold.rs b/tests/by-util/test_fold.rs index 23ac6c989..6cd854016 100644 --- a/tests/by-util/test_fold.rs +++ b/tests/by-util/test_fold.rs @@ -1033,3 +1033,12 @@ fn test_character_mode_special_chars() { .stdout_is(expected); } } + +#[test] +fn test_width_zero() { + new_ucmd!() + .arg("-w") + .arg("0") + .fails_with_code(1) + .stderr_is("fold: illegal width value\n"); +}