fix i32 distance overflow in range length and membership#200
Closed
metsw24-max wants to merge 1 commit into
Closed
fix i32 distance overflow in range length and membership#200metsw24-max wants to merge 1 commit into
metsw24-max wants to merge 1 commit into
Conversation
|
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
approved these changes
Jun 11, 2026
JakobDegen
left a comment
Contributor
There was a problem hiding this comment.
Review automatically exported from Phabricator review in Meta.
|
@JakobDegen merged this pull request in 378d5b3. |
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
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.
Wrong length and membership for ranges spanning the full i32 range
Range::lengthandis_incomputestop - startwithwrapping_subini32and then widen withas u64. Once the span passesi32::MAXthe subtraction wraps to a negative value that sign-extends into a bogusu64, solen(range(-2147483648, 2147483647, 1000))reports 1271310320 rather than 4294968 and5 in range(-2147483648, 2147483647, 7)comes back False. Doing the distance and step-magnitude maths ini64(the wayrem_range_at_iteralready does) keeps the values honest and reads a bit more obviously correct.