Commit 54921aa6 authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto Committed by GitHub

Merge pull request #3289 from bouk/setbyte-len

Read length after args in String#setbyte
parents 99f6de52 83005d83
...@@ -23,10 +23,11 @@ static mrb_value ...@@ -23,10 +23,11 @@ static mrb_value
mrb_str_setbyte(mrb_state *mrb, mrb_value str) mrb_str_setbyte(mrb_state *mrb, mrb_value str)
{ {
mrb_int pos, byte; mrb_int pos, byte;
long len = RSTRING_LEN(str); long len;
mrb_get_args(mrb, "ii", &pos, &byte); mrb_get_args(mrb, "ii", &pos, &byte);
len = RSTRING_LEN(str);
if (pos < -len || len <= pos) if (pos < -len || len <= pos)
mrb_raisef(mrb, E_INDEX_ERROR, "index %S is out of array", mrb_fixnum_value(pos)); mrb_raisef(mrb, E_INDEX_ERROR, "index %S is out of array", mrb_fixnum_value(pos));
if (pos < 0) if (pos < 0)
......
...@@ -30,6 +30,18 @@ assert('String#setbyte') do ...@@ -30,6 +30,18 @@ assert('String#setbyte') do
assert_equal("Hello", str1) assert_equal("Hello", str1)
end end
assert("String#setbyte raises IndexError if arg conversion resizes String") do
$s = "01234\n"
class Tmp
def to_i
$s.chomp! ''
95
end
end
tmp = Tmp.new
assert_raise(IndexError) { $s.setbyte(5, tmp) }
end
assert('String#byteslice') do assert('String#byteslice') do
str1 = "hello" str1 = "hello"
assert_equal("e", str1.byteslice(1)) assert_equal("e", str1.byteslice(1))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment