1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

array.c: fix position in message

* array.c (rb_ary_insert): fix the position in error message, when
  it is less than -1.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58694 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2017-05-12 17:23:46 +00:00
parent e95cac18bd
commit 34761b7f96
2 changed files with 7 additions and 1 deletions

View file

@ -1786,7 +1786,12 @@ rb_ary_insert(int argc, VALUE *argv, VALUE ary)
if (pos == -1) {
pos = RARRAY_LEN(ary);
}
if (pos < 0) {
else if (pos < 0) {
long minpos = -RARRAY_LEN(ary) - 1;
if (pos < minpos) {
rb_raise(rb_eIndexError, "index %ld too small for array; minimum: %ld",
pos, minpos);
}
pos++;
}
rb_ary_splice(ary, pos, 0, argv + 1, argc - 1);

View file

@ -2124,6 +2124,7 @@ class TestArray < Test::Unit::TestCase
assert_raise(TypeError) { a.insert(Object.new) }
assert_equal([0, 1, 2], a.insert(-1, 2))
assert_equal([0, 1, 3, 2], a.insert(-2, 3))
assert_raise_with_message(IndexError, /-6/) { a.insert(-6, 4) }
assert_raise(RuntimeError) { [0].freeze.insert(0)}
assert_raise(ArgumentError) { [0].freeze.insert }
end