[前][次][番号順一覧][スレッド一覧]

ruby-changes:33133

From: nobu <ko1@a...>
Date: Fri, 28 Feb 2014 13:59:54 +0900 (JST)
Subject: [ruby-changes:33133] nobu:r45212 (trunk): numeric.c: integer overflow

nobu	2014-02-28 13:59:49 +0900 (Fri, 28 Feb 2014)

  New Revision: 45212

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=45212

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

  Modified files:
    trunk/numeric.c
    trunk/test/ruby/test_numeric.rb
Index: numeric.c
===================================================================
--- numeric.c	(revision 45211)
+++ numeric.c	(revision 45212)
@@ -1830,7 +1830,7 @@ ruby_num_interval_step_size(VALUE from, https://github.com/ruby/ruby/blob/trunk/numeric.c#L1830
 	if (delta < 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)) {
 	double n = ruby_float_step_size(NUM2DBL(from), NUM2DBL(to), NUM2DBL(step), excl);
Index: test/ruby/test_numeric.rb
===================================================================
--- test/ruby/test_numeric.rb	(revision 45211)
+++ test/ruby/test_numeric.rb	(revision 45212)
@@ -251,7 +251,8 @@ class TestNumeric < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_numeric.rb#L251
   end
 
   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).size }
     assert_raise(ArgumentError) { 1.step(10, 0) { } }
@@ -267,6 +268,9 @@ class TestNumeric < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_numeric.rb#L268
     assert_nothing_raised { 1.step(by: nil) }
     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, to: 10]
     assert_step [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, to: 10, by: nil]

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]