fix(stdlib): return error instead of panicking on out-of-range combinations size 🧮#173
Merged
Merged
Conversation
…ations size 🧮 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f957eb7ec9
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Leaving `first = false` after a failed `ensure_index` relied on a subtle non-local invariant (ensure_index nulling the source) to keep the else branch from indexing the still-empty `indices`. Clear `first` only after `indices` is built, so a re-poll re-enters the first-call branch and returns None idempotently. Adds a re-poll regression case. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
Context
While fuzzing the pipeline for panics, I found that
combinations(seq, k)aborts the process withcapacity overflowfor out-of-rangek:combinations([1, 2, 3], -1)—k as usizeturns-1intousize::MAXcombinations([1, 2, 3], 9223372036854775807)— a genuinely huge positivekBoth reach
CombinationsIter::new, which eagerly built its index vector with(0..k).collect()and asked the allocator for ausize::MAX-elementVec.Changes
ndc_vm:CombinationsIternow storeskand defers theindicesallocation untilensure_index(k - 1)confirms the buffer holds at leastkelements. The allocation is then bounded by data actually materialised, so an over-largekover a short source yields no combinations rather than overflowing. This also covers a large positivek, which the signature change alone does not.ndc_stdlib:combinations,permutations, andsubsequencestookk/lengthasi64and cast withas usize. They now takeusize, so argument conversion rejects a negative value with the same error as the siblingwindows/chunks/takefunctions. Previouslypermutationsandsubsequencessilently returned[]for negative input.Tests
Four regression tests under
tests/functional/programs/900_bugs/: the huge-positive and negative cases forcombinations, plus the negative cases forpermutationsandsubsequences.🤖