Skip to content

Commit 1f8da8d

Browse files
CrazyRokasylvestre
andauthored
ptx: fix panic when reference length exceeds line width (#9816)
Co-authored-by: Sylvestre Ledru <sylvestre@debian.org>
1 parent 27fd411 commit 1f8da8d

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
@@ -788,7 +788,9 @@ fn write_traditional_output(
788788
} else {
789789
0
790790
};
791-
config.line_width -= max_ref_len;
791+
792+
// Use saturating_sub to prevent panic if the reference is wider than the line width.
793+
config.line_width = config.line_width.saturating_sub(max_ref_len);
792794
}
793795

794796
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
@@ -338,3 +338,12 @@ fn test_unicode_truncation_alignment() {
338338
.succeeds()
339339
.stdout_only(" / bar\n föö/\n");
340340
}
341+
342+
#[test]
343+
fn test_narrow_width_with_long_reference_no_panic() {
344+
new_ucmd!()
345+
.args(&["-w", "1", "-A"])
346+
.pipe_in("content")
347+
.succeeds()
348+
.stdout_only(":1 content\n");
349+
}

0 commit comments

Comments
 (0)