summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog12
-rw-r--r--random.c15
-rw-r--r--test/ruby/test_rand.rb8
3 files changed, 34 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index c69957f23e..7da0d9377d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+Sun Feb 24 15:33:46 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * random.c (rb_random_ulong_limited): limit is inclusive, but generic
+ rand method should return a number less than it, so increase for the
+ difference. [ruby-core:52779] [Bug #7935]
+
+Sun Feb 24 15:32:36 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * random.c (rb_random_ulong_limited): limit is inclusive, but generic
+ rand method should return a number less than it, so increase for the
+ difference. [ruby-core:52779] [Bug #7935]
+
Sun Feb 24 15:14:43 2013 Eric Hodel <drbrain@segment7.net>
* lib/net/http.rb: Removed duplicate Accept-Encoding in Net::HTTP#get.
diff --git a/random.c b/random.c
index 9b62cc1add..2ef8959c26 100644
--- a/random.c
+++ b/random.c
@@ -942,13 +942,26 @@ rb_random_real(VALUE obj)
return genrand_real(&rnd->mt);
}
+static inline VALUE
+ulong_to_num_plus_1(unsigned long n)
+{
+#if HAVE_LONG_LONG
+ return ULL2NUM((LONG_LONG)n+1);
+#else
+ if (n >= ULONG_MAX) {
+ return rb_big_plus(ULONG2NUM(n), INT2FIX(1));
+ }
+ return ULONG2NUM(n+1);
+#endif
+}
+
unsigned long
rb_random_ulong_limited(VALUE obj, unsigned long limit)
{
rb_random_t *rnd = try_get_rnd(obj);
if (!rnd) {
extern int rb_num_negative_p(VALUE);
- VALUE lim = ULONG2NUM(limit);
+ VALUE lim = ulong_to_num_plus_1(limit);
VALUE v = rb_funcall2(obj, id_rand, 1, &lim);
unsigned long r = NUM2ULONG(v);
if (rb_num_negative_p(v)) {
diff --git a/test/ruby/test_rand.rb b/test/ruby/test_rand.rb
index 9fabfc607f..611e9f57a0 100644
--- a/test/ruby/test_rand.rb
+++ b/test/ruby/test_rand.rb
@@ -528,5 +528,13 @@ END
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)
+
+ bug7935 = '[ruby-core:52779] [Bug #7935]'
+ class << (gen = Object.new)
+ def rand(limit) @limit = limit; 0 end
+ attr_reader :limit
+ end
+ [1, 2].sample(1, random: gen)
+ assert_equal(2, gen.limit, bug7935)
end
end