diff --git a/string.c b/string.c index 43b8b4e007..be74fd8b72 100644 --- a/string.c +++ b/string.c @@ -4435,12 +4435,14 @@ rb_str_update(VALUE str, long beg, long len, VALUE val) rb_raise(rb_eIndexError, "index %ld out of string", beg); } if (beg < 0) { - if (-beg > slen) { + if (beg + slen < 0) { goto out_of_range; } beg += slen; } - if (slen < len || slen < beg + len) { + assert(beg >= 0); + assert(beg <= slen); + if (len > slen - beg) { len = slen - beg; } str_modify_keep_cr(str); diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb index da8bd5f05a..39cfc9a220 100644 --- a/test/ruby/test_string.rb +++ b/test/ruby/test_string.rb @@ -202,6 +202,8 @@ CODE assert_equal("fobar", s) assert_raise(ArgumentError) { "foo"[1, 2, 3] = "" } + + assert_raise(IndexError) {"foo"[RbConfig::Limits["LONG_MIN"]] = "l"} end def test_CMP # '<=>'