Skip to content

split panics (divide-by-zero) on a zero chunk count in -n l/0 and -n r/0 #12612

@leeewee

Description

@leeewee

Summary

split -n l/0 and split -n r/0 abort with a divide-by-zero panic (SIGABRT, exit 134) instead of rejecting the zero chunk count. The -n argument parser validates N > 0 for the bare N and K/N forms, but the l/N (lines) and r/N (round-robin) forms skip that check, so num_chunks = 0 reaches the chunk math: num_bytes / num_chunks in n_chunks_by_line and i % num_chunks in n_chunks_by_line_round_robin. GNU split rejects 0 up front with an error and exits 1.

Steps to reproduce

$ printf 'a\nb\nc\nd\n' > f
$ split -n l/0 f
thread 'main' panicked at src/uu/split/src/split.rs:1272:27:
attempt to divide by zero
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Aborted (core dumped)
$ echo $?
134

$ split -n r/0 f
thread 'main' panicked at src/uu/split/src/split.rs:1412:47:
attempt to calculate the remainder with a divisor of zero
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Aborted (core dumped)
$ echo $?
134

Expected behavior

Match GNU: report the invalid chunk count and exit non-zero without crashing. GNU validates the count up front (it fails the same way even with no input, e.g. -n l/0 </dev/null), so the fix should reject 0 during argument parsing, not lazily while reading input.

$ /usr/bin/split -n l/0 f
/usr/bin/split: invalid number of chunks: ‘0’: Numerical result out of range
$ echo $?
1
$ /usr/bin/split -n r/0 f
/usr/bin/split: invalid number of chunks: ‘0’: Numerical result out of range
$ echo $?
1

Actual behavior

uutils aborts with a Rust arithmetic panic (panic=abort release build → exit 134). Both the l/N (split into N chunks by line) and r/N (round-robin) forms are affected. The bare N (split -n 0) and K/N byte forms are not affected — those branches already reject 0.

Found by our static analysis tooling.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions