1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-27 19:17:43 +00:00

Merge pull request #7877 from yuankunzhang/main

split: fix a racing condition that causes issue #7869
This commit is contained in:
Daniel Hofstetter 2025-05-04 14:39:09 +02:00 committed by GitHub
commit 69d5cf40b1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 59 additions and 18 deletions

View file

@ -120,6 +120,15 @@ impl RandomFile {
n -= 1;
}
}
/// Add n lines each of the given size.
fn add_lines_with_line_size(&mut self, lines: usize, line_size: usize) {
let mut n = lines;
while n > 0 {
writeln!(self.inner, "{}", random_chars(line_size)).unwrap();
n -= 1;
}
}
}
#[test]
@ -430,6 +439,28 @@ fn test_split_lines_number() {
.stderr_only("split: invalid number of lines: 'file'\n");
}
/// Test interference between split line size and IO buffer capacity.
/// See issue #7869.
#[test]
fn test_split_lines_interfere_with_io_buf_capacity() {
let buf_capacity = BufWriter::new(Vec::new()).capacity();
// We intentionally set the line size to be less than the IO write buffer
// capacity. This is to trigger the condition where after the first split
// file is written, there are still bytes left in the buffer. We then
// test that those bytes are written to the next split file.
let line_size = buf_capacity - 2;
let (at, mut ucmd) = at_and_ucmd!();
let name = "split_lines_interfere_with_io_buf_capacity";
RandomFile::new(&at, name).add_lines_with_line_size(2, line_size);
ucmd.args(&["-l", "1", name]).succeeds();
// Note that `lines_size` doesn't take the trailing newline into account,
// we add 1 for adjustment.
assert_eq!(at.read("xaa").len(), line_size + 1);
assert_eq!(at.read("xab").len(), line_size + 1);
}
/// Test short lines option with value concatenated
#[test]
fn test_split_lines_short_concatenated_with_value() {