summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-08-13 13:13:02 (GMT)
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-08-13 13:13:02 (GMT)
commit26feae3958eff107ce0ff297981f2ed515a19f16 (patch)
tree3714e1f1a96295541ae5656d31f63ba9696ba694
parent944c620dfabbf46e7a96b96b5dad527d985492d3 (diff)
random.c: coerce before check negative
* random.c (rb_random_ulong_limited): coerce before check negative. [Fixes GH-379] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42542 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--random.c2
-rw-r--r--test/ruby/test_array.rb26
3 files changed, 32 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index 475cc3f..c11db01 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Tue Aug 13 22:12:59 2013 Kenichi Kamiya <kachick1@gmail.com>
+
+ * random.c (rb_random_ulong_limited): coerce before check negative.
+ [Fixes GH-379]
+
Tue Aug 13 21:52:15 2013 Kenichi Kamiya <kachick1@gmail.com>
* object.c (Init_Object): undef Module#prepend_features on Class, as
diff --git a/random.c b/random.c
index 6a42e24..cfd2f62 100644
--- a/random.c
+++ b/random.c
@@ -851,7 +851,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 5123ab5..b4f35af 100644
--- a/test/ruby/test_array.rb
+++ b/test/ruby/test_array.rb
@@ -2044,6 +2044,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
@@ -2127,6 +2140,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