Skip to content

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
uutils:mainfrom
AlejandroCoronadoN:fix-split-n-lk-overflow
Open

split: do not overflow on -n l/K when K is larger than the byte count#13903
AlejandroCoronadoN wants to merge 1 commit into
uutils:mainfrom
AlejandroCoronadoN:fix-split-n-lk-overflow

Conversation

@AlejandroCoronadoN

Copy link
Copy Markdown
Contributor

Problem

$ printf 'ab' > tiny
$ split -n l/5 tiny
thread 'main' panicked at src/uu/split/src/split.rs:1144:13: attempt to add with overflow

When there are more chunks than bytes, chunk_size_base = num_bytes / num_chunks
is 0 and the trailing empty chunks add 0 to num_bytes_should_be_written, so the
inner while num_bytes_should_be_written <= num_bytes_written loop never advances
and spins until chunk_number/skipped overflow.

Fix

Add chunk_number < num_chunks to the loop condition so it stops at the last
chunk. There are only num_chunks chunks, so this never truncates real output.

Verification

Compared against GNU split for -n l/5 on ab, -n l/3 on a, -n l/2 on
empty input, -n l/3 on a 10-byte line, and multi-line inputs: the file set and
contents match exactly (xaa gets the data, the rest are empty). Added a
regression test; the full test_split suite (129 tests) passes and cargo fmt
/ cargo clippy are clean.

@codspeed-hq

codspeed-hq Bot commented Aug 13, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 353 untouched benchmarks
⏩ 50 skipped benchmarks1


Comparing AlejandroCoronadoN:fix-split-n-lk-overflow (f4b23f4) with main (dc784af)

Open in CodSpeed

Footnotes

  1. 50 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Comment thread src/uu/split/src/split.rs Outdated
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need 5 five comment lines

@AlejandroCoronadoN

Copy link
Copy Markdown
Contributor Author

Shortened the comment to two lines. Thanks!

@github-actions

Copy link
Copy Markdown

GNU testsuite comparison:

GNU test failed: tests/cp/sparse-to-pipe. tests/cp/sparse-to-pipe is passing on 'main'. Maybe you have to rebase?
Skip an intermittent issue tests/date/date-locale-hour (fails in this run but passes in the 'main' branch)
Skipping an intermittent issue tests/cut/bounded-memory (passes in this run but fails in the 'main' branch)
Skipping an intermittent issue tests/date/resolution (passes in this run but fails in the 'main' branch)
Skipping an intermittent issue tests/pr/bounded-memory (passes in this run but fails in the 'main' branch)
Note: The gnu test tests/seq/seq-epipe is now being skipped but was previously passing.
Note: The gnu test tests/tail/tail-n0f is now being skipped but was previously passing.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants