cat: bugfix when running with -T option

Fixes an crash seen when running with -T option if no newline
is found in a buffer.
Added unit test to validate.
This commit is contained in:
Karl McDowall 2025-04-02 16:56:38 -06:00
parent 3a0b43bdf7
commit a4b621ad8a
2 changed files with 24 additions and 1 deletions

View file

@ -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;
}
};
}
@ -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());

View file

@ -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"] {