From 34761b7f965ec7f2cab4179caa672b32a450dc79 Mon Sep 17 00:00:00 2001 From: nobu Date: Fri, 12 May 2017 17:23:46 +0000 Subject: [PATCH] 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 --- array.c | 7 ++++++- test/ruby/test_array.rb | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) 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