split: do not overflow on -n l/K when K is larger than the byte count - #13903
Open
AlejandroCoronadoN wants to merge 1 commit into
Open
split: do not overflow on -n l/K when K is larger than the byte count#13903AlejandroCoronadoN wants to merge 1 commit into
AlejandroCoronadoN wants to merge 1 commit into
Conversation
Merging this PR will not alter performance
Comparing Footnotes
|
sylvestre
reviewed
Aug 16, 2026
| num_bytes_written += num_line_bytes; | ||
| let mut skipped = -1; | ||
| while num_bytes_should_be_written <= num_bytes_written { | ||
| // Stop once the last chunk is reached. When there are more chunks than |
Contributor
There was a problem hiding this comment.
I don't think we need 5 five comment lines
AlejandroCoronadoN
force-pushed
the
fix-split-n-lk-overflow
branch
from
August 17, 2026 14:24
eed3fdb to
f4b23f4
Compare
Contributor
Author
|
Shortened the comment to two lines. Thanks! |
|
GNU testsuite comparison: |
split -n l/K with more chunks than bytes panicked with an integer overflow: the trailing empty chunks never advance the loop, so it spins until chunk_number and skipped overflow. Bound the loop by the chunk count so the extra chunks are written as empty files, matching GNU.
AlejandroCoronadoN
force-pushed
the
fix-split-n-lk-overflow
branch
from
August 19, 2026 19:36
f4b23f4 to
154f1fa
Compare
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.
Problem
When there are more chunks than bytes,
chunk_size_base = num_bytes / num_chunksis 0 and the trailing empty chunks add 0 to
num_bytes_should_be_written, so theinner
while num_bytes_should_be_written <= num_bytes_writtenloop never advancesand spins until
chunk_number/skippedoverflow.Fix
Add
chunk_number < num_chunksto the loop condition so it stops at the lastchunk. There are only
num_chunkschunks, so this never truncates real output.Verification
Compared against GNU
splitfor-n l/5onab,-n l/3ona,-n l/2onempty input,
-n l/3on a 10-byte line, and multi-line inputs: the file set andcontents match exactly (
xaagets the data, the rest are empty). Added aregression test; the full
test_splitsuite (129 tests) passes andcargo fmt/
cargo clippyare clean.