summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--array.c7
-rw-r--r--test/ruby/test_array.rb1
2 files changed, 7 insertions, 1 deletions
diff --git a/array.c b/array.c
index 1eb1d2ea35..cff6d69ad3 100644
--- a/array.c
+++ b/array.c
@@ -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);
diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb
index 60938937cd..d1a570099f 100644
--- a/test/ruby/test_array.rb
+++ b/test/ruby/test_array.rb
@@ -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