-
Notifications
You must be signed in to change notification settings - Fork 1
fix(stdlib): return error instead of panicking on out-of-range combinations size 🧮 #173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
23 changes: 23 additions & 0 deletions
23
tests/functional/programs/900_bugs/bug0027_combinations_huge_k.ndc
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // `combinations(seq, k)` built its index vector eagerly with | ||
| // `(0..k).collect()`. A huge `k` (e.g. `i64::MAX`) over a short source | ||
| // asked the allocator for an `i64::MAX`-element `Vec<usize>` and aborted | ||
| // with "capacity overflow". The index vector is now only allocated once | ||
| // the buffer is confirmed to hold at least `k` elements, so an | ||
| // over-large `k` simply yields no combinations instead of panicking. | ||
| assert_eq(combinations([1, 2, 3], 9223372036854775807).list(), []); | ||
|
|
||
| // `k` larger than the source length yields nothing (boundary just above n). | ||
| assert_eq(combinations([1, 2, 3], 4).list(), []); | ||
|
|
||
| // Re-polling an exhausted over-large combination stays empty rather than | ||
| // indexing a never-initialised index vector. Covers both eager and lazy | ||
| // (enumerate) sources, drained twice. | ||
| let eager = combinations([1, 2, 3], 5); | ||
| assert_eq(eager.list(), []); | ||
| assert_eq(eager.list(), []); | ||
| let lazy = combinations([1, 2, 3].enumerate(), 5); | ||
| assert_eq(lazy.list(), []); | ||
| assert_eq(lazy.list(), []); | ||
|
|
||
| // Valid combinations are still produced correctly. | ||
| assert_eq([1, 2, 3, 4].combinations(2).list(), [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]); |
7 changes: 7 additions & 0 deletions
7
tests/functional/programs/900_bugs/bug0028_combinations_negative_k.ndc
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| // `combinations(seq, k)` took `k` as an `i64` and cast it with `k as usize`, | ||
| // turning a negative `k` into `usize::MAX` and aborting with "capacity | ||
| // overflow" while building the index vector. `k` is now a `usize` argument, | ||
| // so a negative value is rejected by argument conversion with a graceful | ||
| // error — matching the sibling `windows`/`chunks`/`take` functions. | ||
| // expect-error: expected a non-negative integer, but the value was negative | ||
| combinations([1, 2, 3], -1); |
5 changes: 5 additions & 0 deletions
5
tests/functional/programs/900_bugs/bug0029_permutations_negative_k.ndc
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| // `permutations(seq, k)` cast `k as usize`, silently turning a negative `k` | ||
| // into `usize::MAX` and returning an empty list. `k` is now a `usize` | ||
| // argument, so a negative value is rejected with a graceful error instead. | ||
| // expect-error: expected a non-negative integer, but the value was negative | ||
| permutations([1, 2, 3], -1); |
6 changes: 6 additions & 0 deletions
6
tests/functional/programs/900_bugs/bug0030_subsequences_negative_length.ndc
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| // `subsequences(seq, length)` cast `length as usize`, silently turning a | ||
| // negative `length` into `usize::MAX` and returning an empty list. `length` | ||
| // is now a `usize` argument, so a negative value is rejected with a graceful | ||
| // error instead. | ||
| // expect-error: expected a non-negative integer, but the value was negative | ||
| subsequences([1, 2, 3], -1); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.