diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-02-26 05:41:37 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-02-26 05:41:37 +0000 |
commit | 60dad06d53757ece68be7bcd1968462cf1dee18a (patch) | |
tree | 675268d4e5556aaebe516dde79fc3f61c17bfddf | |
parent | a12e8fd2ed5fa6cb4b30b28e96c2442587a7aef8 (diff) | |
download | ruby-60dad06d53757ece68be7bcd1968462cf1dee18a.tar.gz |
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
-rw-r--r-- | ChangeLog | 6 | ||||
-rw-r--r-- | numeric.c | 30 | ||||
-rw-r--r-- | test/ruby/test_float.rb | 2 | ||||
-rw-r--r-- | test/ruby/test_numeric.rb | 4 |
4 files changed, 38 insertions, 4 deletions
@@ -1,3 +1,9 @@ +Fri Feb 26 14:40:48 2016 Nobuyoshi Nakada <nobu@ruby-lang.org> + + * 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 <nobu@ruby-lang.org> * doc/extension.rdoc, doc/extension.ja.rdoc: add editor local @@ -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) } |