1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

random.c: increase limit for generic rand

* 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]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@39466 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2013-02-24 06:33:49 +00:00
parent 023561f704
commit d64c926eba
3 changed files with 34 additions and 1 deletions

View file

@ -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.

View file

@ -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)) {

View file

@ -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