split: fix overflow on a huge -n l/K/N chunk count - #14374
Merged
sylvestre merged 1 commit intoSep 4, 2026
Conversation
`split -n l/K/N` advances to the next chunk one chunk at a time. Once the input is shorter than the number of chunks, the trailing chunks are all zero-sized, so the loop makes no progress on the byte count and keeps counting up to N. The counter for the chunks it stepped over was inferred as `i32`, so a large N overflowed it and panicked under overflow checks. Skip to the last chunk as soon as a zero-sized one is reached, since every chunk after it is zero-sized too, and derive the number of chunks stepped over from the chunk number itself so there is no separate counter to overflow.
|
GNU testsuite comparison: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was wrong
split -n l/K/N FILEpanicked withattempt to add with overflow(exit 101) under overflow checks whenNwas very large:A release build wrapped the counter silently instead, but still spent minutes spinning before it finished.
Root cause
n_chunks_by_lineadvances to the next chunk one chunk at a time. When the input is shorter than the requested number of chunks, every trailing chunk is zero-sized, sonum_bytes_should_be_writtenstops growing and the loop condition stays true untilchunk_numberreachesnum_chunks— up toNiterations. The count of chunks stepped over waslet mut skipped = -1;, which is inferred asi32, so it overflowed onceNpassedi32::MAX.Just widening the counter is not enough:
Nis parsed as au64, so it can exceedi64::MAX, and the loop would still step through allNchunks — effectively forever forNnearu64::MAX.The fix
skippedcounter and take the--elide-empty-filesrollback from the chunk number captured before the loop (chunk_number.min(first_chunk_number + 1)). This is equivalent to the previouschunk_number -= skippedand leaves no counter that can overflow.Output is unchanged:
-n l/1/9999999999on a short input now produces what-n l/1/3,-n l/1/100and-n l/1/1000000already produced, and the existing elide-empty-files behaviour is untouched.How it was tested
test_number_by_lines_kth_huge_number_of_chunksintests/by-util/test_split.rs. Onmainit fails (the run is killed by the 30s test timeout; run by hand the binary panics and exits 101), and it passes with this change.cargo test --no-default-features --features split --test tests test_split→ 119 passed, 0 failed.split -n l/1/9999999999 bignow exits 0 immediately with the full 100000 bytes, andsplit -n l/1/18446744073709551615 bigworks as well.cargo clippy -p uu_split --all-targetsis clean,cargo fmtreports no changes.Fixes #14328