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

ruby-changes:30463

From: nobu <ko1@a...>
Date: Tue, 13 Aug 2013 22:13:09 +0900 (JST)
Subject: [ruby-changes:30463] nobu:r42542 (trunk): random.c: coerce before check negative

nobu	2013-08-13 22:13:02 +0900 (Tue, 13 Aug 2013)

  New Revision: 42542

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

  Log:
    random.c: coerce before check negative
    
    * random.c (rb_random_ulong_limited): coerce before check negative.
      [Fixes GH-379]

  Modified files:
    trunk/ChangeLog
    trunk/random.c
    trunk/test/ruby/test_array.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 42541)
+++ ChangeLog	(revision 42542)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Tue Aug 13 22:12:59 2013  Kenichi Kamiya  <kachick1@g...>
+
+	* random.c (rb_random_ulong_limited): coerce before check negative.
+	  [Fixes GH-379]
+
 Tue Aug 13 21:52:15 2013  Kenichi Kamiya  <kachick1@g...>
 
 	* object.c (Init_Object): undef Module#prepend_features on Class, as
Index: test/ruby/test_array.rb
===================================================================
--- test/ruby/test_array.rb	(revision 42541)
+++ test/ruby/test_array.rb	(revision 42542)
@@ -2044,6 +2044,19 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L2044
       alias rand call
     end
     assert_raise(RuntimeError) {ary.shuffle!(random: gen)}
+
+    zero = Object.new
+    def zero.to_int
+      0
+    end
+    gen_to_int = proc do |max|
+      zero
+    end
+    class << gen_to_int
+      alias rand call
+    end
+    ary = (0...10000).to_a
+    assert_equal(ary.rotate, ary.shuffle(random: gen_to_int))
   end
 
   def test_sample
@@ -2127,6 +2140,19 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L2140
     assert_equal([5000, 0, 5001, 2, 5002, 4, 5003, 6, 5004, 8, 5005], ary.sample(11, random: gen0))
     ary.sample(11, random: gen1) # implementation detail, may change in the future
     assert_equal([], ary)
+
+    half = Object.new
+    def half.to_int
+      5000
+    end
+    gen_to_int = proc do |max|
+      half
+    end
+    class << gen_to_int
+      alias rand call
+    end
+    ary = (0...10000).to_a
+    assert_equal(5000, ary.sample(random: gen_to_int))
   end
 
   def test_cycle
Index: random.c
===================================================================
--- random.c	(revision 42541)
+++ random.c	(revision 42542)
@@ -851,7 +851,7 @@ rb_random_ulong_limited(VALUE obj, unsig https://github.com/ruby/ruby/blob/trunk/random.c#L851
     if (!rnd) {
 	extern int rb_num_negative_p(VALUE);
 	VALUE lim = ulong_to_num_plus_1(limit);
-	VALUE v = rb_funcall2(obj, id_rand, 1, &lim);
+	VALUE v = rb_to_int(rb_funcall2(obj, id_rand, 1, &lim));
 	unsigned long r = NUM2ULONG(v);
 	if (rb_num_negative_p(v)) {
 	    rb_raise(rb_eRangeError, "random number too small %ld", r);

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

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