1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 03:57:44 +00:00

split: handle no final newline with --line-bytes

Fix a panic due to out-of-bounds indexing when using `split
--line-bytes` with an input that had no trailing newline.
This commit is contained in:
Jeffrey Finkelstein 2022-03-19 23:50:02 -04:00
parent 1d17400b80
commit 95f58fbf3c
2 changed files with 17 additions and 1 deletions

View file

@ -886,7 +886,7 @@ impl<'a> Write for LineBytesChunkWriter<'a> {
// then move on to the next chunk if necessary.
None => {
let end = self.num_bytes_remaining_in_current_chunk;
let num_bytes_written = self.inner.write(&buf[..end])?;
let num_bytes_written = self.inner.write(&buf[..end.min(buf.len())])?;
self.num_bytes_remaining_in_current_chunk -= num_bytes_written;
total_bytes_written += num_bytes_written;
buf = &buf[num_bytes_written..];

View file

@ -618,3 +618,19 @@ fn test_line_bytes() {
assert_eq!(at.read("xac"), "cccc\ndd\n");
assert_eq!(at.read("xad"), "ee\n");
}
#[test]
fn test_line_bytes_no_final_newline() {
let (at, mut ucmd) = at_and_ucmd!();
ucmd.args(&["-C", "2"])
.pipe_in("1\n2222\n3\n4")
.succeeds()
.no_stdout()
.no_stderr();
assert_eq!(at.read("xaa"), "1\n");
assert_eq!(at.read("xab"), "22");
assert_eq!(at.read("xac"), "22");
assert_eq!(at.read("xad"), "\n");
assert_eq!(at.read("xae"), "3\n");
assert_eq!(at.read("xaf"), "4");
}