From 05b00a14c5081ee4bf362b12839dcbb94df1169a Mon Sep 17 00:00:00 2001 From: h2so5 Date: Wed, 17 Dec 2014 14:01:39 +0900 Subject: String#[] should reject nil index --- src/string.c | 1 + test/t/string.rb | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/src/string.c b/src/string.c index 1c54ffa72..2b1a27fee 100644 --- a/src/string.c +++ b/src/string.c @@ -781,6 +781,7 @@ num_index: } } default: + if (mrb_nil_p(indx)) mrb_raise(mrb, E_TYPE_ERROR, "can't convert nil into Integer"); idx = mrb_fixnum(indx); goto num_index; } diff --git a/test/t/string.rb b/test/t/string.rb index a56862fe2..63e4af000 100644 --- a/test/t/string.rb +++ b/test/t/string.rb @@ -72,6 +72,10 @@ assert('String#[]', '15.2.10.5.6') do assert_equal 'bc', e1 assert_equal 'bc', a3 assert_nil b3 + + assert_raise(TypeError) do + a[nil] + end end assert('String#[] with Range') do -- cgit v1.2.3 From 4319eaaa45ce26e73cfac6e1c8b724e35680577b Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Wed, 17 Dec 2014 20:08:26 +0900 Subject: try to convert not only nil but every objects to fixnums; ref #2677 --- src/string.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/string.c b/src/string.c index 2b1a27fee..aa26b261c 100644 --- a/src/string.c +++ b/src/string.c @@ -781,7 +781,10 @@ num_index: } } default: - if (mrb_nil_p(indx)) mrb_raise(mrb, E_TYPE_ERROR, "can't convert nil into Integer"); + indx = mrb_Integer(mrb, indx); + if (mrb_nil_p(indx)) { + mrb_raise(mrb, E_TYPE_ERROR, "can't convert to Fixnum"); + } idx = mrb_fixnum(indx); goto num_index; } -- cgit v1.2.3