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

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 degrade performance by 7.83%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 1 improved benchmark
❌ 1 regressed benchmark
✅ 73 untouched benchmarks
⏩ 318 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation df_with_path 573.7 µs 704.9 µs -18.62%
Simulation du_summarize_balanced_tree[(5, 4, 10)] 16.8 ms 16.1 ms +4.38%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing AlejandroCoronadoN:fix-split-n-lk-overflow (eed3fdb) with main (822aa83)

Open in CodSpeed

Footnotes

  1. 318 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

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

Copy link
Copy Markdown
Author

Shortened the comment to two lines. Thanks!

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