Commit d25d343e authored by Yukihiro "Matz" Matsumoto's avatar Yukihiro "Matz" Matsumoto

Merge pull request #2208 from yui-knk/array-error

Make Array#[]= raise IndexError.
parents 09b9c776 eafc4dd0
...@@ -561,6 +561,10 @@ mrb_ary_splice(mrb_state *mrb, mrb_value ary, mrb_int head, mrb_int len, mrb_val ...@@ -561,6 +561,10 @@ mrb_ary_splice(mrb_state *mrb, mrb_value ary, mrb_int head, mrb_int len, mrb_val
mrb_int i, argc; mrb_int i, argc;
ary_modify(mrb, a); ary_modify(mrb, a);
/* len check */
if (len < 0) mrb_raisef(mrb, E_INDEX_ERROR, "negative length (%S)", mrb_fixnum_value(len));
/* range check */ /* range check */
if (head < 0) { if (head < 0) {
head += a->len; head += a->len;
......
...@@ -66,6 +66,11 @@ assert('Array#[]=', '15.2.12.5.5') do ...@@ -66,6 +66,11 @@ assert('Array#[]=', '15.2.12.5.5') do
# this will cause an exception due to the wrong arguments # this will cause an exception due to the wrong arguments
a.[]=(1,2,3,4) a.[]=(1,2,3,4)
end end
assert_raise(IndexError) do
# this will cause an exception due to the wrong arguments
a = [1,2,3,4,5]
a[1, -1] = 10
end
assert_equal(4, [1,2,3].[]=(1,4)) assert_equal(4, [1,2,3].[]=(1,4))
assert_equal(3, [1,2,3].[]=(1,2,3)) assert_equal(3, [1,2,3].[]=(1,2,3))
......
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