You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
diff panics on an oversized inline context count (-c/-u/-C/-U): parse overflow and capacity overflow #245
diff with an inline numeric context count whose digits exceed usize::MAX — e.g. -u99999999999999999999, -c99999999999999999999, --context=…, --unified=…, -C…, -U… — panics and aborts (exit 134). The option parser captures the digit run with a regex, then does numvalue.as_str().parse::<usize>().unwrap(); an out-of-range value makes parse return Err(PosOverflow) and the bare .unwrap() aborts. GNU diff accepts the oversized count (clamps it) and produces the diff normally, exit 1.
Steps to reproduce
$ printf'a\n'> f1;printf'b\n'> f2
$ diffutils diff -u99999999999999999999 f1 f2thread 'main' panicked at src/params.rs:323:73:called `Result::unwrap()` on an `Err` value: ParseIntError { kind: PosOverflow }note: run with `RUST_BACKTRACE=1` environment variable to display a backtraceAborted (core dumped)
$ echo$?134
The context (-c/-C/--context) forms hit the sibling site src/params.rs:279:
$ diffutils diff -c99999999999999999999 f1 f2thread 'main' panicked at src/params.rs:279:73:called `Result::unwrap()` on an `Err` value: ParseIntError { kind: PosOverflow }
$ echo$?134
Root cause
parse_params in src/params.rs captures the count with a regex \d+/\d*
that does not bound the digit-run length, then unwraps the parse:
// src/params.rs:279 (context) and :323 (unified)let context = numvalue.as_str().parse::<usize>().unwrap();
A digit string larger than usize::MAX (e.g. 99999999999999999999) parses to Err(ParseIntError { kind: PosOverflow }), and the unconditional .unwrap() aborts.
A second panic in the same family: a context count that fits in usize but is large enough that N * 16 > isize::MAX (~N > 5.76e17) aborts in VecDeque::with_capacity rather than the parse path.
$ printf 'a\nb\nc\n' > A; printf 'a\nX\nc\n' > B
$ diff -U600000000000000000AB # same with -C
thread 'main' panicked at src/unified_diff.rs:47:46:capacity overflow # exit 134
src/unified_diff.rs:47 and src/context_diff.rs:56 both do VecDeque::with_capacity(context_size) with the count unbounded. GNU clamps to the file length (exit 1); clamping context_size fixes both panics.
changed the title [-]diff panics (integer parse overflow) on an oversized inline context count (`-c<N>`/`-u<N>`/`-C<N>`/`-U<N>`)[/-][+]diff panics on an oversized inline context count (`-c`/`-u`/`-C`/`-U`): parse overflow and capacity overflow[/+]on Jun 16, 2026
Activity
leeewee commentedon Jun 16, 2026
A second panic in the same family: a context count that fits in usize but is large enough that
N * 16 > isize::MAX (~N > 5.76e17)aborts inVecDeque::with_capacityrather than the parse path.src/unified_diff.rs:47andsrc/context_diff.rs:56both doVecDeque::with_capacity(context_size)with the count unbounded. GNU clamps to the file length (exit 1); clampingcontext_sizefixes both panics.[-]diff panics (integer parse overflow) on an oversized inline context count (`-c<N>`/`-u<N>`/`-C<N>`/`-U<N>`)[/-][+]diff panics on an oversized inline context count (`-c`/`-u`/`-C`/`-U`): parse overflow and capacity overflow[/+]diff: don't panic on an oversized context count