Repository navigation
Convert str indices without casting the char length to i32 - #255
Closed
Dilshad-Tanvi wants to merge 1 commit into
Closed
Dilshad-Tanvi wants to merge 1 commit into
Dilshad-Tanvi wants to merge 1 commit into
Conversation
Contributor
|
This pull request has been imported. If you are a Meta employee, you can view this in D127104746. (Because this pull request was imported automatically, there will not be any future comments.) |
JakobDegen
approved these changes
Oct 23, 2026
JakobDegen
left a comment
Contributor
There was a problem hiding this comment.
Review automatically exported from Phabricator review in Meta.
meta-codesync Bot
pushed a commit
to facebook/buck2
that referenced
this pull request
Oct 23, 2026
Summary:
`convert_str_indices_slow`, the negative-index path behind str slicing and `find`/`rfind`/`index`/`rindex`/`count`/`startswith`/`endswith`, casts the char length to `i32` before calling `convert_indices`. A string can hold up to `u32::MAX` bytes, so at 2^31 chars or more the length wraps negative, `bound` returns `limit as usize` (about 2^64) for a positive index, and the ASCII fast path passes that to `get_unchecked(start..end)`. In a release build `s = ("a" * (1 << 30)) * 2; s.find("b", -2147483648, 5)` builds a `&str` running far past the 2 GiB buffer and segfaults reading it. `s[:-1]` builds the same out-of-bounds slice and only stops at the `len overflow` assert in `alloc_str`.
Convert the indices in `usize` against the real char length, and make the `end <= len` precondition of the unchecked slice an `assert!` instead of a `debug_assert!`. With the fix the script above returns `-1`. Both the bytes and the list/tuple slicing paths use `apply_slice` with checked indexing, so this is the only unchecked slice fed by the wrapped length. The new unit test drives the index conversion with a `1 << 31` length directly so it doesn't need a 2 GiB allocation.
X-link: facebook/starlark-rust#255
Reviewed By: JakobDegen
Differential Revision: D127104746
fbshipit-source-id: f41e47335a0719274872e02761f283b64c335477
Contributor
|
This pull request has been merged in c6e2f40. |
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.
convert_str_indices_slow, the negative-index path behind str slicing andfind/rfind/index/rindex/count/startswith/endswith, casts the char length toi32before callingconvert_indices. A string can hold up tou32::MAXbytes, so at 2^31 chars or more the length wraps negative,boundreturnslimit as usize(about 2^64) for a positive index, and the ASCII fast path passes that toget_unchecked(start..end). In a release builds = ("a" * (1 << 30)) * 2; s.find("b", -2147483648, 5)builds a&strrunning far past the 2 GiB buffer and segfaults reading it.s[:-1]builds the same out-of-bounds slice and only stops at thelen overflowassert inalloc_str.Convert the indices in
usizeagainst the real char length, and make theend <= lenprecondition of the unchecked slice anassert!instead of adebug_assert!. With the fix the script above returns-1. Both the bytes and the list/tuple slicing paths useapply_slicewith checked indexing, so this is the only unchecked slice fed by the wrapped length. The new unit test drives the index conversion with a1 << 31length directly so it doesn't need a 2 GiB allocation.