Sitelet https://github.com/starkware-libs/cairo/pull/10022
Skip to content

fix(corelib): Take::nth(usize::MAX) returns None instead of overflowing - #10022

Merged
orizi merged 1 commit into
mainfrom
orizi/take-nth-overflow
Jun 1, 2026
Merged

orizi merged 1 commit into
mainfrom
orizi/take-nth-overflow

Conversation

@orizi

@orizi orizi commented Jun 1, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes an integer overflow in Take::nth() when called with n = usize::MAX. Previously, the implementation computed n + 1 directly before calling checked_sub, which would overflow when n is usize::MAX. The fix splits the operation into two checked steps: first using checked_add(1) on n, then passing the result to checked_sub, so that overflow is handled safely and None is returned instead of panicking or producing incorrect results.


Type of change

Please check one:

  • Bug fix (fixes incorrect behavior)
  • New feature
  • Performance improvement
  • Documentation change with concrete technical impact
  • Style, wording, formatting, or typo-only change

Why is this change needed?

Calling iter.take(n).nth(usize::MAX) would overflow when computing n + 1 before the checked subtraction, causing incorrect behavior instead of safely returning None.


What was the behavior or documentation before?

Take::nth(usize::MAX) would overflow on the n + 1 arithmetic, since the addition was performed before any overflow check.


What is the behavior or documentation after?

Take::nth(usize::MAX) now correctly returns None without overflowing, because n.checked_add(1) is evaluated first and short-circuits the rest of the expression on overflow.


Related issue or discussion (if any)

None.


Additional context

A regression test was added to iter_test.cairo to assert that nth(usize::MAX) returns None on a Take iterator, preventing this overflow from being reintroduced.

`self.n.checked_sub(n + 1)` evaluated `n + 1` before the bounds check, so it
overflowed for `n == usize::MAX`. Use `n.checked_add(1)` in a let-chain so an
overflow falls through to the not-enough-elements branch (None) rather than
panicking. Add a regression case to test_iter_adapter_take_nth.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

orizi commented Jun 1, 2026

Copy link
Copy Markdown
Collaborator Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@orizi
orizi requested a review from eytan-starkware June 1, 2026 11:35
@orizi
orizi marked this pull request as ready for review June 1, 2026 11:35
@cursor

cursor Bot commented Jun 1, 2026 •

Copy link
Copy Markdown

PR Summary

Low Risk
Small, localized iterator adapter fix with a targeted test; no auth, I/O, or API surface changes beyond safer arithmetic.

Overview
Fixes an overflow in Take::nth when n is usize::MAX. The adapter used self.n.checked_sub(n + 1), so n + 1 could wrap before the check; it now uses n.checked_add(1) and only subtracts when that succeeds, matching the existing “exhaust take budget” fallback path and returning None safely.

A regression test in iter_test.cairo asserts take(5).nth(usize::MAX) is None.

Reviewed by Cursor Bugbot for commit e0dc81d. Bugbot is set up for automated code reviews on this repo. Configure here.

@eytan-starkware eytan-starkware left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

@eytan-starkware reviewed 2 files and all commit messages, and made 1 comment.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on orizi).

@orizi
orizi added this pull request to the merge queue Jun 1, 2026
Merged via the queue into main with commit 06a9b14 Jun 1, 2026
54 checks passed
@orizi
orizi deleted the orizi/take-nth-overflow branch June 1, 2026 14:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants