Skip to content

Commit 6d75472

Browse files
committed
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.
1 parent 3a0b43b commit 6d75472

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/uu/cat/src/cat.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ fn write_tab_to_end<W: Write>(mut in_buf: &[u8], writer: &mut W) -> usize {
644644
}
645645
None => {
646646
writer.write_all(in_buf).unwrap();
647-
return in_buf.len();
647+
return in_buf.len() + count;
648648
}
649649
};
650650
}
@@ -688,6 +688,22 @@ fn write_end_of_line<W: Write>(
688688
mod tests {
689689
use std::io::{BufWriter, stdout};
690690

691+
#[test]
692+
fn test_write_tab_to_end_new_line() {
693+
let mut writer = BufWriter::with_capacity(1024 * 64, stdout());
694+
let in_buf = b"a\tb\tc\n";
695+
let tab = b"";
696+
assert_eq!(super::write_tab_to_end(in_buf, &mut writer), 5);
697+
}
698+
699+
#[test]
700+
fn test_write_tab_to_end_no_new_line() {
701+
let mut writer = BufWriter::with_capacity(1024 * 64, stdout());
702+
let in_buf = b"a\tb\tc";
703+
let tab = b"";
704+
assert_eq!(super::write_tab_to_end(in_buf, &mut writer), 5);
705+
}
706+
691707
#[test]
692708
fn test_write_nonprint_to_end_new_line() {
693709
let mut writer = BufWriter::with_capacity(1024 * 64, stdout());

0 commit comments

Comments
 (0)