diff --git a/src/uu/fmt/src/fmt.rs b/src/uu/fmt/src/fmt.rs index 960b20185..6f104c458 100644 --- a/src/uu/fmt/src/fmt.rs +++ b/src/uu/fmt/src/fmt.rs @@ -34,6 +34,7 @@ mod parasplit; static SYNTAX: &str = "[OPTION]... [FILE]..."; static SUMMARY: &str = "Reformat paragraphs from input files (or stdin) to stdout."; static LONG_HELP: &str = ""; +static MAX_WIDTH: usize = 2500; pub type FileOrStdReader = BufReader>; pub struct FmtOptions { @@ -137,6 +138,13 @@ pub fn uumain(args: impl uucore::Args) -> i32 { crash!(1, "Invalid WIDTH specification: `{}': {}", s, e); } }; + if fmt_opts.width > MAX_WIDTH { + crash!( + 1, + "invalid width: '{}': Numerical result out of range", + fmt_opts.width + ); + } fmt_opts.goal = cmp::min(fmt_opts.width * 94 / 100, fmt_opts.width - 3); }; diff --git a/tests/by-util/test_fmt.rs b/tests/by-util/test_fmt.rs index e7990a760..4533cdf24 100644 --- a/tests/by-util/test_fmt.rs +++ b/tests/by-util/test_fmt.rs @@ -20,6 +20,19 @@ fn test_fmt_q() { ); } +#[test] +fn test_fmt_w_too_big() { + let result = new_ucmd!() + .arg("-w") + .arg("2501") + .arg("one-word-per-line.txt") + .run(); + //.stdout_is_fixture("call_graph.expected"); + assert_eq!( + result.stderr.trim(), + "fmt: error: invalid width: '2501': Numerical result out of range" + ); +} /* #[test] Fails for now, see https://github.com/uutils/coreutils/issues/1501 fn test_fmt_w() {