Skip to content

Commit cb3ac65

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 cb3ac65

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/uu/cat/src/cat.rs

Lines changed: 15 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,20 @@ 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+
assert_eq!(super::write_tab_to_end(in_buf, &mut writer), 5);
696+
}
697+
698+
#[test]
699+
fn test_write_tab_to_end_no_new_line() {
700+
let mut writer = BufWriter::with_capacity(1024 * 64, stdout());
701+
let in_buf = b"a\tb\tc";
702+
assert_eq!(super::write_tab_to_end(in_buf, &mut writer), 5);
703+
}
704+
691705
#[test]
692706
fn test_write_nonprint_to_end_new_line() {
693707
let mut writer = BufWriter::with_capacity(1024 * 64, stdout());

0 commit comments

Comments
 (0)