From 6327810b03ba3ac899f54581f2448fd792640b15 Mon Sep 17 00:00:00 2001 From: Misakait Date: Thu, 2 Oct 2025 16:41:50 +0800 Subject: [PATCH] fix(ptx): Prevent text chunks from over-reading at boundaries Fixes a bug in `get_output_chunks` where calculated text chunks (e.g., `tail`) would incorrectly include a trailing space and the first character of the next word. The fix explicitly checks for and trims this pattern to ensure chunk boundaries are respected, matching GNU ptx behavior. --- src/uu/ptx/src/ptx.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/uu/ptx/src/ptx.rs b/src/uu/ptx/src/ptx.rs index 98317d725..f3afc58df 100644 --- a/src/uu/ptx/src/ptx.rs +++ b/src/uu/ptx/src/ptx.rs @@ -484,7 +484,18 @@ fn get_output_chunks( let tail_end = trim_broken_word_right(all_after, tail_beg, tail_end); // trim away whitespace again. - let (tail_beg, tail_end) = trim_idx(all_after, tail_beg, tail_end); + let (tail_beg, mut tail_end) = trim_idx(all_after, tail_beg, tail_end); + // Fix: Manually trim trailing char (like "a") that are preceded by a space. + // This handles cases like "is a" which are not correctly trimmed by the + // preceding functions. + if tail_end >= 2 + && (tail_end - 2) > tail_beg + && all_after[tail_end - 2].is_whitespace() + && !all_after[tail_end - 1].is_whitespace() + { + tail_end -= 1; + (_, tail_end) = trim_idx(all_after, tail_beg, tail_end); + } // and get the string let tail_str: String = all_after[tail_beg..tail_end].iter().collect();