summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornagachika <nagachika@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-09-28 13:04:48 +0000
committernagachika <nagachika@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-09-28 13:04:48 +0000
commite5bcf05d664fcb7e564bb95b2817a29c810fb7ef (patch)
tree5ed216a3507099903e6dcbea1f253207b4e596e8
parent87791df311cac2ee31dc884d39f42126519cdb87 (diff)
merge revision(s) 42542: [Backport #8910]
* random.c (rb_random_ulong_limited): coerce before check negative. [Fixes GH-379] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_0_0@43079 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--random.c2
-rw-r--r--test/ruby/test_array.rb26
-rw-r--r--version.h6
4 files changed, 35 insertions, 4 deletions
diff --git a/ChangeLog b/ChangeLog
index 8644219cc9..0ff3a4d463 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Sat Sep 28 21:40:40 2013 Kenichi Kamiya <kachick1@gmail.com>
+
+ * random.c (rb_random_ulong_limited): coerce before check negative.
+ [Fixes GH-379]
+
Fri Sep 27 01:24:20 2013 CHIKANAGA Tomoyuki <nagachika@ruby-lang.org>
* lib/rubygems: Update to Rubygems 2.0.10. [ruby-core:57360]
diff --git a/random.c b/random.c
index da83c2c867..4cb6aee773 100644
--- a/random.c
+++ b/random.c
@@ -965,7 +965,7 @@ rb_random_ulong_limited(VALUE obj, unsigned long limit)
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);
diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb
index a8b514c830..71b514419f 100644
--- a/test/ruby/test_array.rb
+++ b/test/ruby/test_array.rb
@@ -2020,6 +2020,19 @@ class TestArray < Test::Unit::TestCase
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
@@ -2103,6 +2116,19 @@ class TestArray < Test::Unit::TestCase
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
diff --git a/version.h b/version.h
index cdd99e7474..644d74816f 100644
--- a/version.h
+++ b/version.h
@@ -1,10 +1,10 @@
#define RUBY_VERSION "2.0.0"
-#define RUBY_RELEASE_DATE "2013-09-27"
-#define RUBY_PATCHLEVEL 321
+#define RUBY_RELEASE_DATE "2013-09-28"
+#define RUBY_PATCHLEVEL 322
#define RUBY_RELEASE_YEAR 2013
#define RUBY_RELEASE_MONTH 9
-#define RUBY_RELEASE_DAY 27
+#define RUBY_RELEASE_DAY 28
#include "ruby/version.h"