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

ruby-changes:27622

From: nagachika <ko1@a...>
Date: Sat, 9 Mar 2013 23:52:54 +0900 (JST)
Subject: [ruby-changes:27622] nagachika:r39674 (ruby_2_0_0): merge revision(s) 39376: [Backport #7903]

nagachika	2013-03-09 23:52:33 +0900 (Sat, 09 Mar 2013)

  New Revision: 39674

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

  Log:
    merge revision(s) 39376: [Backport #7903]
    
    * random.c (rb_random_ulong_limited): fix error message for negative
      value.  [ruby-dev:47061] [Bug #7903]

  Modified directories:
    branches/ruby_2_0_0/
  Modified files:
    branches/ruby_2_0_0/ChangeLog
    branches/ruby_2_0_0/internal.h
    branches/ruby_2_0_0/numeric.c
    branches/ruby_2_0_0/random.c
    branches/ruby_2_0_0/test/ruby/test_rand.rb
    branches/ruby_2_0_0/version.h

Index: ruby_2_0_0/ChangeLog
===================================================================
--- ruby_2_0_0/ChangeLog	(revision 39673)
+++ ruby_2_0_0/ChangeLog	(revision 39674)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/ChangeLog#L1
+Sat Mar  9 23:51:58 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* random.c (rb_random_ulong_limited): fix error message for negative
+	  value.  [ruby-dev:47061] [Bug #7903]
+
 Sat Mar  9 23:41:11 2013  Zachary Scott  <zachary@z...>
 
 	* thread.c: Document ThreadGroup::Default
Index: ruby_2_0_0/numeric.c
===================================================================
--- ruby_2_0_0/numeric.c	(revision 39673)
+++ ruby_2_0_0/numeric.c	(revision 39674)
@@ -185,6 +185,12 @@ negative_int_p(VALUE num) https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/numeric.c#L185
     return RTEST(rb_funcall(num, mid, 1, INT2FIX(0)));
 }
 
+int
+rb_num_negative_p(VALUE num)
+{
+    return negative_int_p(num);
+}
+
 /*
  *  call-seq:
  *     num.coerce(numeric)  ->  array
Index: ruby_2_0_0/internal.h
===================================================================
--- ruby_2_0_0/internal.h	(revision 39673)
+++ ruby_2_0_0/internal.h	(revision 39674)
@@ -168,6 +168,7 @@ int rb_num_to_uint(VALUE val, unsigned i https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/internal.h#L168
 VALUE num_interval_step_size(VALUE from, VALUE to, VALUE step, int excl);
 int ruby_float_step(VALUE from, VALUE to, VALUE step, int excl);
 double ruby_float_mod(double x, double y);
+int rb_num_negative_p(VALUE);
 
 /* object.c */
 VALUE rb_obj_equal(VALUE obj1, VALUE obj2);
Index: ruby_2_0_0/version.h
===================================================================
--- ruby_2_0_0/version.h	(revision 39673)
+++ ruby_2_0_0/version.h	(revision 39674)
@@ -1,6 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/version.h#L1
 #define RUBY_VERSION "2.0.0"
 #define RUBY_RELEASE_DATE "2013-03-09"
-#define RUBY_PATCHLEVEL 43
+#define RUBY_PATCHLEVEL 44
 
 #define RUBY_RELEASE_YEAR 2013
 #define RUBY_RELEASE_MONTH 3
Index: ruby_2_0_0/test/ruby/test_rand.rb
===================================================================
--- ruby_2_0_0/test/ruby/test_rand.rb	(revision 39673)
+++ ruby_2_0_0/test/ruby/test_rand.rb	(revision 39674)
@@ -1,4 +1,5 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/test/ruby/test_rand.rb#L1
 require 'test/unit'
+require_relative 'envutil'
 
 class TestRand < Test::Unit::TestCase
   def assert_random_int(ws, m, init = 0)
@@ -514,4 +515,18 @@ END https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/test/ruby/test_rand.rb#L515
       l.call
     end
   end
+
+  def test_random_ulong_limited
+    def (gen = Object.new).rand(*) 1 end
+    assert_equal([2], (1..100).map {[1,2,3].sample(random: gen)}.uniq)
+
+    def (gen = Object.new).rand(*) 100 end
+    e = assert_raise(RangeError) {[1,2,3].sample(random: gen)}
+    assert_match(/big 100\z/, e.message)
+
+    bug7903 = '[ruby-dev:47061] [Bug #7903]'
+    def (gen = Object.new).rand(*) -1 end
+    e = assert_raise(RangeError) {[1,2,3].sample(random: gen)}
+    assert_match(/small -1\z/, e.message, bug7903)
+  end
 end
Index: ruby_2_0_0/random.c
===================================================================
--- ruby_2_0_0/random.c	(revision 39673)
+++ ruby_2_0_0/random.c	(revision 39674)
@@ -947,9 +947,13 @@ rb_random_ulong_limited(VALUE obj, unsig https://github.com/ruby/ruby/blob/trunk/ruby_2_0_0/random.c#L947
 {
     rb_random_t *rnd = try_get_rnd(obj);
     if (!rnd) {
+	extern int rb_num_negative_p(VALUE);
 	VALUE lim = ULONG2NUM(limit);
 	VALUE v = 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);
+	}
 	if (r > limit) {
 	    rb_raise(rb_eRangeError, "random number too big %ld", r);
 	}

Property changes on: ruby_2_0_0
___________________________________________________________________
Modified: svn:mergeinfo
   Merged /trunk:r39376


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

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