Skip to content

Take long indexes in String#[], slice and byteslice - #10075

Merged
headius merged 1 commit into
jruby:masterfrom
sampokuokkanen:string-aref-long-index
Nov 17, 2026
Merged

headius merged 1 commit into
jruby:masterfrom
sampokuokkanen:string-aref-long-index

Conversation

@sampokuokkanen

Copy link
Copy Markdown
Contributor

String#[], slice and single-argument byteslice narrowed their index and length to int, so an Integer outside int range gave wrong results:

"abc"[2**40]             # JRuby "a" (2**40 truncated to 0), CRuby nil
"abc"[1, 2**31-1]        # JRuby OutOfMemoryError, CRuby "bc"
"abc"[0, 2**40]          # JRuby RangeError, CRuby "abc"
"abc"[0..2**40]          # JRuby RangeError, CRuby "abc"
"abc".byteslice(2.0**40) # JRuby RangeError, CRuby nil
s = +"hello"; s.slice!(2**33 + 1) # JRuby "e" and s == "hllo", CRuby nil

This change takes long values and bounds them by the byte length, as CRuby does in rb_str_subpos (called from rb_str_aref) and str_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 an int. The length checks now use len > length - beg instead of beg + len > length, which overflowed. slice! with a single index goes through [], so it is fixed too.

Tests: new examples in spec/ruby/core/string for [], slice! and multibyte strings (they also pass on CRuby 4.0.7 and fail on JRuby master), and four String#[]/slice tags and the test_AREF_underflow excludes are removed. spec/ruby/core/string passes in interpreter and JIT modes, and test/mri/ruby/test_string.rb passes.

Not fixed here: []= still narrows to int, so a single index outside int range is truncated. The index/length and Range forms of []= and slice! raise RangeError.

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

kares commented Nov 17, 2026

Copy link
Copy Markdown
Member

does this affect performance in any way, not that I would expect, just wondering if you did some checks?

@headius headius left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

@headius headius added this to the JRuby 10.1.4.0 milestone Nov 17, 2026
@headius
headius merged commit 6cba11e into jruby:master Nov 17, 2026
146 checks passed
@sampokuokkanen

Copy link
Copy Markdown
Contributor Author

@kares
Doesn't seem to be much of a difference:

Call Before (Mops/s) After (Mops/s) After / before
s[3] 38.5 38.0 0.99x
s[-3] 38.1 37.6 0.99x
s[3, 5] 33.9 33.1 0.98x
s[3..8] 30.2 33.6 1.11x
s.byteslice(3) 30.2 30.4 1.00x
s.byteslice(3, 5) 43.2 43.7 1.01x
u[3, 5] (multibyte) 20.3 20.3 1.00x

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