Repository navigation
Take long indexes in String#[], slice and byteslice - #10075
Merged
Merged
Conversation
String#[] narrowed its index and length to int, so an Integer outside
int range went wrong:
"abc"[2**40] # "a" (2**40 truncated to 0), CRuby nil
"abc"[1, 2**31-1] # OutOfMemoryError (beg + len overflowed)
"abc"[0, 2**40] # RangeError, CRuby "abc"
"abc"[0..2**40] # RangeError, CRuby "abc"
Like rb_str_substr, take longs and bound them by the byte length: a
start outside it is out of range for any encoding, and no substring is
longer. substrEnc and byteSubstr now check len > length - beg instead
of beg + len > length, which overflowed for strings over 1 GiB or a
length near Long.MAX_VALUE. Ranges use the long RubyRange#begLen.
byteslice takes a long index too, so byteslice(2.0**40) is nil.
slice! with a single index goes through [], so it is fixed too.
[]= still narrows to int: a single index outside int range truncates
like [] did, and the index/length and Range forms of []= and slice!
raise RangeError. That is left for a follow-up.
Member
|
does this affect performance in any way, not that I would expect, just wondering if you did some checks? |
headius
approved these changes
Nov 17, 2026
headius
left a comment
Member
There was a problem hiding this comment.
Nice! This seems a clean way to handle long inputs; if they eventually would be accepted for a given length string, allow them through as longs even though we can't dereference long indices. The byte[] is always limited to a 32-bit signed integer, so only raise range errors when handling the long value would require addressing outside that range.
Contributor
Author
|
@kares
|
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.
String#[],sliceand single-argumentbyteslicenarrowed their index and length toint, so an Integer outsideintrange gave wrong results:This change takes
longvalues and bounds them by the byte length, as CRuby does inrb_str_subpos(called fromrb_str_aref) andstr_byte_aref. A start past the byte length is out of range in any encoding, and no substring is longer than the byte length, so after the bound both values fit in anint. The length checks now uselen > length - beginstead ofbeg + len > length, which overflowed.slice!with a single index goes through[], so it is fixed too.Tests: new examples in
spec/ruby/core/stringfor[],slice!and multibyte strings (they also pass on CRuby 4.0.7 and fail on JRuby master), and fourString#[]/slicetags and thetest_AREF_underflowexcludes are removed.spec/ruby/core/stringpasses in interpreter and JIT modes, andtest/mri/ruby/test_string.rbpasses.Not fixed here:
[]=still narrows toint, so a single index outsideintrange is truncated. The index/length and Range forms of[]=andslice!raise RangeError.