Skip to content

Commit 92b2e8d

Browse files
committed
ptx: fix panic when reference length exceeds line width
1 parent cc103ec commit 92b2e8d

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/uu/ptx/src/ptx.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,9 @@ fn write_traditional_output(
747747
} else {
748748
0
749749
};
750-
config.line_width -= max_ref_len;
750+
751+
// Use saturating_sub to prevent panic if the reference is wider than the line width.
752+
config.line_width = config.line_width.saturating_sub(max_ref_len);
751753
}
752754

753755
for word_ref in words {

tests/by-util/test_ptx.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,12 @@ fn test_unicode_truncation_alignment() {
301301
.succeeds()
302302
.stdout_only(" / bar\n föö/\n");
303303
}
304+
305+
#[test]
306+
fn test_narrow_width_with_long_reference_no_panic() {
307+
new_ucmd!()
308+
.args(&["-w", "1", "-A"])
309+
.pipe_in("content")
310+
.succeeds()
311+
.stdout_only(":1 content\n");
312+
}

0 commit comments

Comments
 (0)