1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

numeric.c: integer overflow

* numeric.c (ruby_num_interval_step_size): get rid of integer
  overflow.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45212 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2014-02-28 04:59:49 +00:00
parent d596ba8dc0
commit a4a551f856
2 changed files with 6 additions and 2 deletions

View file

@ -1830,7 +1830,7 @@ ruby_num_interval_step_size(VALUE from, VALUE to, VALUE step, int excl)
if (delta < 0) { if (delta < 0) {
return INT2FIX(0); return INT2FIX(0);
} }
return LONG2FIX(delta / diff + 1); return ULONG2NUM(delta / diff + 1UL);
} }
else if (RB_TYPE_P(from, T_FLOAT) || RB_TYPE_P(to, T_FLOAT) || RB_TYPE_P(step, T_FLOAT)) { else if (RB_TYPE_P(from, T_FLOAT) || RB_TYPE_P(to, T_FLOAT) || RB_TYPE_P(step, T_FLOAT)) {
double n = ruby_float_step_size(NUM2DBL(from), NUM2DBL(to), NUM2DBL(step), excl); double n = ruby_float_step_size(NUM2DBL(from), NUM2DBL(to), NUM2DBL(step), excl);

View file

@ -251,7 +251,8 @@ class TestNumeric < Test::Unit::TestCase
end end
def test_step def test_step
bignum = 1 << 100 i, bignum = 32, 1 << 30
bignum <<= (i <<= 1) - 32 until bignum.is_a?(Bignum)
assert_raise(ArgumentError) { 1.step(10, 1, 0) { } } assert_raise(ArgumentError) { 1.step(10, 1, 0) { } }
assert_raise(ArgumentError) { 1.step(10, 1, 0).size } assert_raise(ArgumentError) { 1.step(10, 1, 0).size }
assert_raise(ArgumentError) { 1.step(10, 0) { } } assert_raise(ArgumentError) { 1.step(10, 0) { } }
@ -267,6 +268,9 @@ class TestNumeric < Test::Unit::TestCase
assert_nothing_raised { 1.step(by: nil) } assert_nothing_raised { 1.step(by: nil) }
assert_nothing_raised { 1.step(by: nil).size } assert_nothing_raised { 1.step(by: nil).size }
assert_equal(bignum*2+1, (-bignum).step(bignum, 1).size)
assert_equal(bignum*2, (-bignum).step(bignum-1, 1).size)
assert_step [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 10] assert_step [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 10]
assert_step [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, to: 10] assert_step [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, to: 10]
assert_step [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, to: 10, by: nil] assert_step [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, to: 10, by: nil]