Skip to content

Take long indexes in String#[]=, slice! and insert - #10104

Open
sampokuokkanen wants to merge 1 commit into
jruby:masterfrom
sampokuokkanen:string-aset-long-index
Open

sampokuokkanen wants to merge 1 commit into
jruby:masterfrom
sampokuokkanen:string-aset-long-index

Conversation

@sampokuokkanen

@sampokuokkanen sampokuokkanen commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #10075, same fix for String#[]=, slice! and insert:

s = +"abc"; s[2**40] = "x"    # s == "xbc", CRuby IndexError
s = +"abc"; s[0, 2**40] = "x" # RangeError, CRuby s == "x"
"abc".slice!(0..2**40)        # RangeError, CRuby "abc"

StringSupport.strUpdate now takes longs and checks them like rb_str_update. Range forms use the long RubyRange#begLen, whose RangeError message now matches begLenInt.

Not fixed: getbyte, setbyte, byteindex and bytesplice still use int. (+"a") << (2**32 + 65) appends "A" instead of raising RangeError.

[]= narrowed its index and length to int, so an Integer outside int
range went wrong:

    s = +"abc"; s[2**40] = "x"   # s == "xbc", CRuby IndexError
    s = +"abc"; s[0, 2**40] = "x" # RangeError, CRuby s == "x"
    s = +"abc"; s[0..2**40] = "x" # RangeError, CRuby s == "x"
    "abc".slice!(0, 2**40)        # RangeError, CRuby "abc"
    "abc".insert(2**40, "x")      # RangeError, CRuby IndexError

Like rb_str_update, StringSupport.strUpdate now takes longs and checks
them against the character length, so both fit in an int afterwards.
The int overload delegates to it. Ranges use the long RubyRange#begLen,
whose RangeError message now shows the range as given, like begLenInt.

getbyte, setbyte, byteindex and bytesplice still narrow to int.
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.

1 participant