Need type check for replace by String#bytesplice

This commit is contained in:
dearblue
2023-04-15 21:25:57 +09:00
parent 2e9ac8f448
commit 73b6c6619c
2 changed files with 4 additions and 0 deletions
+1
View File
@@ -3006,6 +3006,7 @@ mrb_str_bytesplice(mrb_state *mrb, mrb_value str)
mrb_get_args(mrb, "iiS", &idx1, &len1, &replace);
return str_bytesplice(mrb, str, idx1, len1, replace, 0, RSTRING_LEN(replace));
}
mrb_ensure_string_type(mrb, replace);
if (mrb_range_beg_len(mrb, range1, &idx1, &len1, RSTRING_LEN(str), FALSE) != MRB_RANGE_OK) break;
if (mrb_range_beg_len(mrb, range2, &idx2, &len2, RSTRING_LEN(replace), FALSE) != MRB_RANGE_OK) break;
return str_bytesplice(mrb, str, idx1, len1, replace, idx2, len2);
+3
View File
@@ -948,4 +948,7 @@ assert('String#bytesplice') do
# idx, len, replace, idx, len (len1<len2)
a = "0123456789"
assert_equal "0cd23456789", a.bytesplice(1, 1, b, 2, 2)
# check the object type to replace
assert_raise(TypeError) { "0123456789".bytesplice(1, 1, Object.new) }
end