diff: don't panic on an oversized context count - #274
Open
MsfPablo wants to merge 1 commit into
Open
Conversation
|
GNU diffutils testsuite comparison: |
An inline count like -u99999999999999999999 reached `parse::<usize>().unwrap()` and aborted with a PosOverflow. A count that fits in usize but is large enough that N * 16 exceeds isize::MAX aborted later instead, in VecDeque::with_capacity. Saturate the parse, and clamp context_size to the combined input length in both make_diff implementations. A file cannot have more lines than bytes, so anything larger already meant the whole file; the clamp keeps the surrounding arithmetic in range and stops the preallocation from scaling with a number the caller picked. Fixes uutils#245
sylvestre
force-pushed
the
fix-oversized-context
branch
from
August 18, 2026 06:55
e852df3 to
8428c8f
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.
Fixes #245.
Both panics in the issue come from the context count being used unchecked:
-u99999999999999999999and friends reachnumvalue.as_str().parse::<usize>().unwrap()inparams.rs, andPosOverflowaborts the process.N * 16 > isize::MAXgets past the parser and aborts inVecDeque::with_capacityinstead, which is the second case @leeewee added in the comments.The parse now saturates, and
make_diffclampscontext_sizeto the combined length of the two inputs. Neither file can have more lines than it has bytes, so a request above that already meant "the whole file" — the clamp only removes values that were never reachable, and it keeps both the preallocation and thecontext_size + 1arithmetic below it in range. That covers the unified and context paths in one place rather than guarding each site.All six affected spellings now produce the same output as a plain
-u/-crun and exit 1.Tests: a unit test in
params.rsfor the saturating parse, and an integration test running each spelling end to end.One thing I left alone: the separate-argument forms (
-C 99999999999999999999) already returninvalid context lengthrather than panicking. GNU accepts those, so the behaviour still differs, but since it is not a crash I did not want to fold a compatibility change into this fix. Happy to do it separately if you want them aligned.