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
This commit is contained in:
nobu 2016-02-26 05:41:37 +00:00
Родитель a12e8fd2ed
Коммит 60dad06d53
4 изменённых файлов: 38 добавлений и 4 удалений

Просмотреть файл

@ -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);
}

Просмотреть файл

@ -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

Просмотреть файл

@ -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) }