diff --git a/src/uu/ptx/src/ptx.rs b/src/uu/ptx/src/ptx.rs index 28d19cdbdfd..7a520e8cbeb 100644 --- a/src/uu/ptx/src/ptx.rs +++ b/src/uu/ptx/src/ptx.rs @@ -747,7 +747,9 @@ fn write_traditional_output( } else { 0 }; - config.line_width -= max_ref_len; + + // Use saturating_sub to prevent panic if the reference is wider than the line width. + config.line_width = config.line_width.saturating_sub(max_ref_len); } for word_ref in words { diff --git a/tests/by-util/test_ptx.rs b/tests/by-util/test_ptx.rs index c9ecb5c22e2..64dd4cd6a81 100644 --- a/tests/by-util/test_ptx.rs +++ b/tests/by-util/test_ptx.rs @@ -301,3 +301,12 @@ fn test_unicode_truncation_alignment() { .succeeds() .stdout_only(" / bar\n föö/\n"); } + +#[test] +fn test_narrow_width_with_long_reference_no_panic() { + new_ucmd!() + .args(&["-w", "1", "-A"]) + .pipe_in("content") + .succeeds() + .stdout_only(":1 content\n"); +}