Skip to content
Open
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
4 changes: 3 additions & 1 deletion src/uu/split/src/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,9 @@ fn n_chunks_by_line(
let num_line_bytes = bytes.len() as u64;
num_bytes_written += num_line_bytes;
let mut skipped = -1;
while num_bytes_should_be_written <= num_bytes_written {
// Stop at the last chunk: with more chunks than bytes the increment is 0,
// so without this bound the loop spins until chunk_number/skipped overflow.
while num_bytes_should_be_written <= num_bytes_written && chunk_number < num_chunks {
num_bytes_should_be_written +=
chunk_size_base + (chunk_size_reminder > chunk_number) as u64;
chunk_number += 1;
Expand Down
13 changes: 13 additions & 0 deletions tests/by-util/test_split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,19 @@ fn test_split_num_prefixed_chunks_by_lines() {
assert_eq!(glob.collate(), at.read_bytes(name));
}

#[test]
fn test_number_of_chunks_by_line_more_chunks_than_bytes() {
// More chunks than bytes must not overflow; the trailing chunks are empty.
let (at, mut ucmd) = at_and_ucmd!();
at.write("in", "ab");
ucmd.args(&["-n", "l/5", "in"]).succeeds().no_stderr();
assert_eq!(at.read("xaa"), "ab");
for name in ["xab", "xac", "xad", "xae"] {
assert_eq!(at.read(name), "");
}
assert!(!at.file_exists("xaf"));
}

#[test]
fn test_split_str_prefixed_chunks_by_lines() {
let (at, mut ucmd) = at_and_ucmd!();
Expand Down