Skip to content

fix i32 distance overflow in range length and membership#200

Closed
metsw24-max wants to merge 1 commit into
facebook:mainfrom
metsw24-max:range-distance-i64
Closed

fix i32 distance overflow in range length and membership#200
metsw24-max wants to merge 1 commit into
facebook:mainfrom
metsw24-max:range-distance-i64

Conversation

@metsw24-max

Copy link
Copy Markdown
Contributor

Wrong length and membership for ranges spanning the full i32 range

Range::length and is_in compute stop - start with wrapping_sub in i32 and then widen with as u64. Once the span passes i32::MAX the subtraction wraps to a negative value that sign-extends into a bogus u64, so len(range(-2147483648, 2147483647, 1000)) reports 1271310320 rather than 4294968 and 5 in range(-2147483648, 2147483647, 7) comes back False. Doing the distance and step-magnitude maths in i64 (the way rem_range_at_iter already does) keeps the values honest and reads a bit more obviously correct.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jun 9, 2026
@meta-codesync

meta-codesync Bot commented Jun 9, 2026

Copy link
Copy Markdown

This pull request has been imported. If you are a Meta employee, you can view this in D107970420. (Because this pull request was imported automatically, there will not be any future comments.)

@JakobDegen JakobDegen 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.

Review automatically exported from Phabricator review in Meta.

@meta-codesync meta-codesync Bot closed this in 378d5b3 Jun 14, 2026
@meta-codesync

meta-codesync Bot commented Jun 14, 2026

Copy link
Copy Markdown

@JakobDegen merged this pull request in 378d5b3.

@meta-codesync meta-codesync Bot added the Merged label Jun 14, 2026
meta-codesync Bot pushed a commit to facebook/buck2 that referenced this pull request Jun 14, 2026
Summary:
**Wrong length and membership for ranges spanning the full i32 range**

`Range::length` and `is_in` compute `stop - start` with `wrapping_sub` in `i32` and then widen with `as u64`. Once the span passes `i32::MAX` the subtraction wraps to a negative value that sign-extends into a bogus `u64`, so `len(range(-2147483648, 2147483647, 1000))` reports 1271310320 rather than 4294968 and `5 in range(-2147483648, 2147483647, 7)` comes back False. Doing the distance and step-magnitude maths in `i64` (the way `rem_range_at_iter` already does) keeps the values honest and reads a bit more obviously correct.

X-link: facebook/starlark-rust#200

Reviewed By: IanChilds

Differential Revision: D107970420

Pulled By: JakobDegen

fbshipit-source-id: fc3dc13e9c35cc1ec088450894ffaf28aefc98dc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants