mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 03:57:44 +00:00
split: elide all chunks when input file is empty
Fix a bug in the behavior of `split -e -n NUM` when the input file is empty. Previously, it would panic due to overflow when subtracting 1 from 0. After this change, it will terminate successfully and produce no output chunks.
This commit is contained in:
parent
ad237f8fff
commit
0a226524a6
2 changed files with 20 additions and 0 deletions
|
@ -984,6 +984,13 @@ where
|
||||||
(num_chunks, chunk_size)
|
(num_chunks, chunk_size)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// If we would have written zero chunks of output, then terminate
|
||||||
|
// immediately. This happens on `split -e -n 3 /dev/null`, for
|
||||||
|
// example.
|
||||||
|
if num_chunks == 0 {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
let num_chunks: usize = num_chunks
|
let num_chunks: usize = num_chunks
|
||||||
.try_into()
|
.try_into()
|
||||||
.map_err(|_| USimpleError::new(1, "Number of chunks too big"))?;
|
.map_err(|_| USimpleError::new(1, "Number of chunks too big"))?;
|
||||||
|
|
|
@ -571,6 +571,19 @@ fn test_elide_empty_files() {
|
||||||
assert!(!at.plus("xad").exists());
|
assert!(!at.plus("xad").exists());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(unix)]
|
||||||
|
fn test_elide_dev_null() {
|
||||||
|
let (at, mut ucmd) = at_and_ucmd!();
|
||||||
|
ucmd.args(&["-e", "-n", "3", "/dev/null"])
|
||||||
|
.succeeds()
|
||||||
|
.no_stdout()
|
||||||
|
.no_stderr();
|
||||||
|
assert!(!at.plus("xaa").exists());
|
||||||
|
assert!(!at.plus("xab").exists());
|
||||||
|
assert!(!at.plus("xac").exists());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_lines() {
|
fn test_lines() {
|
||||||
let (at, mut ucmd) = at_and_ucmd!();
|
let (at, mut ucmd) = at_and_ucmd!();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue