mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
Prohibit string lengths less than 0 in String#bytesplice
This commit is contained in:
@@ -2956,6 +2956,9 @@ str_bytesplice(mrb_state *mrb, mrb_value str, mrb_int idx1, mrb_int len1, mrb_va
|
||||
if (RSTR_LEN(s) < idx1 || idx1 < 0 || RSTRING_LEN(replace) < idx2 || idx2 < 0) {
|
||||
mrb_raise(mrb, E_INDEX_ERROR, "index out of string");
|
||||
}
|
||||
if (len1 < 0 || len2 < 0) {
|
||||
mrb_raise(mrb, E_INDEX_ERROR, "negative length");
|
||||
}
|
||||
mrb_int n;
|
||||
if (mrb_int_add_overflow(idx1, len1, &n) || RSTR_LEN(s) < n) {
|
||||
len1 = RSTR_LEN(s) - idx1;
|
||||
|
||||
@@ -960,4 +960,7 @@ assert('String#bytesplice') do
|
||||
assert_equal "0ab3456789", "0123456789".bytesplice(-9, 2, "ab")
|
||||
assert_equal "ab23456789", "0123456789".bytesplice(-10, 2, "ab")
|
||||
assert_raise(IndexError) { "0123456789".bytesplice(-11, 2, "ab") }
|
||||
|
||||
# check the negative length
|
||||
assert_raise(IndexError) { "0123456789".bytesplice(3, -4, "ab") }
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user