From 60dad06d53757ece68be7bcd1968462cf1dee18a Mon Sep 17 00:00:00 2001 From: nobu Date: Fri, 26 Feb 2016 05:41:37 +0000 Subject: numeric.c: wrong type step should raise TypeError * numeric.c (num_step_scan_args): comparison String with Numeric should raise TypeError. it is an invalid type, but not a mismatch the number of arguments. [ruby-core:62430] [Bug #9810] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53949 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 6 ++++++ numeric.c | 30 +++++++++++++++++++++++++++++- test/ruby/test_float.rb | 2 +- test/ruby/test_numeric.rb | 4 ++-- 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 68270c3ba0..05df84058c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Fri Feb 26 14:40:48 2016 Nobuyoshi Nakada + + * numeric.c (num_step_scan_args): comparison String with Numeric + should raise TypeError. it is an invalid type, but not a + mismatch the number of arguments. [ruby-core:62430] [Bug #9810] + Fri Feb 26 14:39:39 2016 Nobuyoshi Nakada * doc/extension.rdoc, doc/extension.ja.rdoc: add editor local diff --git a/numeric.c b/numeric.c index 607a72d756..c424203109 100644 --- a/numeric.c +++ b/numeric.c @@ -2081,6 +2081,34 @@ ruby_num_interval_step_size(VALUE from, VALUE to, VALUE step, int excl) } } +static VALUE +num_step_compare_with_zero(VALUE num) +{ + VALUE zero = INT2FIX(0); + return rb_check_funcall(num, '>', 1, &zero); +} + +static int +num_step_negative_p(VALUE num) +{ + const ID mid = '<'; + VALUE r; + + if (FIXNUM_P(num)) { + if (method_basic_p(rb_cFixnum)) + return (SIGNED_VALUE)num < 0; + } + else if (RB_TYPE_P(num, T_BIGNUM)) { + if (method_basic_p(rb_cBignum)) + return BIGNUM_NEGATIVE_P(num); + } + r = rb_rescue(num_step_compare_with_zero, num, coerce_rescue_quiet, Qnil); + if (r == Qundef) { + coerce_failed(num, INT2FIX(0)); + } + return !RTEST(r); +} + static int num_step_scan_args(int argc, const VALUE *argv, VALUE *to, VALUE *step) { @@ -2115,7 +2143,7 @@ num_step_scan_args(int argc, const VALUE *argv, VALUE *to, VALUE *step) if (NIL_P(*step)) { *step = INT2FIX(1); } - desc = !positive_int_p(*step); + desc = num_step_negative_p(*step); if (NIL_P(*to)) { *to = desc ? DBL2NUM(-INFINITY) : DBL2NUM(INFINITY); } diff --git a/test/ruby/test_float.rb b/test/ruby/test_float.rb index 3afd26d0b4..aeac359081 100644 --- a/test/ruby/test_float.rb +++ b/test/ruby/test_float.rb @@ -601,7 +601,7 @@ class TestFloat < Test::Unit::TestCase end def test_num2dbl - assert_raise(ArgumentError) do + assert_raise(TypeError) do 1.0.step(2.0, "0.5") {} end assert_raise(TypeError) do diff --git a/test/ruby/test_numeric.rb b/test/ruby/test_numeric.rb index 5c55539d91..1710cf8d84 100644 --- a/test/ruby/test_numeric.rb +++ b/test/ruby/test_numeric.rb @@ -262,8 +262,8 @@ class TestNumeric < Test::Unit::TestCase assert_raise(ArgumentError) { 1.step(10, 1, 0).size } assert_raise(ArgumentError) { 1.step(10, 0) { } } assert_raise(ArgumentError) { 1.step(10, 0).size } - assert_raise(ArgumentError) { 1.step(10, "1") { } } - assert_raise(ArgumentError) { 1.step(10, "1").size } + assert_raise(TypeError) { 1.step(10, "1") { } } + assert_raise(TypeError) { 1.step(10, "1").size } assert_raise(TypeError) { 1.step(10, nil) { } } assert_raise(TypeError) { 1.step(10, nil).size } assert_nothing_raised { 1.step(by: 0, to: nil) } -- cgit v1.2.3