diff --git a/src/uu/fmt/src/fmt.rs b/src/uu/fmt/src/fmt.rs index dd8f10fd882..06cac3cb3ea 100644 --- a/src/uu/fmt/src/fmt.rs +++ b/src/uu/fmt/src/fmt.rs @@ -141,7 +141,10 @@ impl FmtOptions { (0, 0) } (Some(w), None) => { - let g = (w * DEFAULT_GOAL_TO_WIDTH_RATIO / 100).max(1); + let g = match w.checked_mul(DEFAULT_GOAL_TO_WIDTH_RATIO) { + Some(result) => (result / 100).max(1), + None => { Err(FmtError::InvalidWidth(w.to_string())) }?, + }; (w, g) } (None, Some(g)) => { diff --git a/tests/by-util/test_fmt.rs b/tests/by-util/test_fmt.rs index b223da5bc5c..8953cfe8a4e 100644 --- a/tests/by-util/test_fmt.rs +++ b/tests/by-util/test_fmt.rs @@ -428,3 +428,11 @@ fn test_fmt_invalid_utf8() { .succeeds() .stdout_is_bytes(b"=\xA0=\n"); } + +#[test] +fn test_fmt_width_multiplication_overflow() { + new_ucmd!() + .args(&["-w", "267672676527678256"]) + .fails_with_code(1) + .stderr_is("fmt: invalid width: '267672676527678256'\n"); +}