Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/uu/cat/src/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ fn write_tab_to_end<W: Write>(mut in_buf: &[u8], writer: &mut W) -> usize {
}
None => {
writer.write_all(in_buf).unwrap();
return in_buf.len();
return in_buf.len() + count;
Copy link
Contributor Author

@karlmcdowall karlmcdowall Apr 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 638 above moves the in_buf slice along the original input in_buf. If the function later returns because no \r or \n characters were found (i.e. we match to line 645) then we would return just the size of the final slice, when in fact we should be returning the size of the entire buffer.

}
};
}
Expand Down Expand Up @@ -688,6 +688,20 @@ fn write_end_of_line<W: Write>(
mod tests {
use std::io::{BufWriter, stdout};

#[test]
fn test_write_tab_to_end_with_newline() {
let mut writer = BufWriter::with_capacity(1024 * 64, stdout());
let in_buf = b"a\tb\tc\n";
assert_eq!(super::write_tab_to_end(in_buf, &mut writer), 5);
}

#[test]
fn test_write_tab_to_end_no_newline() {
let mut writer = BufWriter::with_capacity(1024 * 64, stdout());
let in_buf = b"a\tb\tc";
assert_eq!(super::write_tab_to_end(in_buf, &mut writer), 5);
}

#[test]
fn test_write_nonprint_to_end_new_line() {
let mut writer = BufWriter::with_capacity(1024 * 64, stdout());
Expand Down
9 changes: 9 additions & 0 deletions tests/by-util/test_cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,15 @@ fn test_stdin_nonprinting_and_tabs_repeated() {
.stdout_only("^I^@\n");
}

#[test]
fn test_stdin_tabs_no_newline() {
new_ucmd!()
.args(&["-T"])
.pipe_in("\ta")
.succeeds()
.stdout_only("^Ia");
}

#[test]
fn test_stdin_squeeze_blank() {
for same_param in ["-s", "--squeeze-blank", "--squeeze"] {
Expand Down
Loading