summaryrefslogtreecommitdiff
path: root/random.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-02-14 04:11:26 (GMT)
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-02-14 04:11:26 (GMT)
commitdd09b5bf1d0b45f6d74df49ad1deea9762036b10 (patch)
treed7f796363fffced5ca0313b475ff75c4ea285b2b /random.c
parentb1adbd14e5b197596636f927c03ff132299ab6b6 (diff)
random.c: allow negative argument to random_number
* random.c (rand_random_number): allow negative argument as it is allowed by SecureRandom.random_number. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49597 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'random.c')
-rw-r--r--random.c25
1 files changed, 17 insertions, 8 deletions
diff --git a/random.c b/random.c
index 47246db..a48573a 100644
--- a/random.c
+++ b/random.c
@@ -1070,6 +1070,13 @@ domain_error(void)
rb_exc_raise(rb_class_new_instance(1, &error, rb_eSystemCallError));
}
+NORETURN(static void invalid_argument(VALUE));
+static void
+invalid_argument(VALUE arg0)
+{
+ rb_raise(rb_eArgError, "invalid argument - %"PRIsVALUE, arg0);
+}
+
static inline double
float_value(VALUE v)
{
@@ -1190,7 +1197,9 @@ static VALUE rand_random(int argc, VALUE *argv, VALUE obj, rb_random_t *rnd);
static VALUE
random_rand(int argc, VALUE *argv, VALUE obj)
{
- return rand_random(argc, argv, obj, get_rnd(obj));
+ VALUE v = rand_random(argc, argv, obj, get_rnd(obj));
+ if (NIL_P(v)) invalid_argument(argv[0]);
+ return v;
}
static VALUE
@@ -1236,18 +1245,16 @@ rand_random(int argc, VALUE *argv, VALUE obj, rb_random_t *rnd)
v = Qnil;
(void)NUM2LONG(vmax);
}
- if (NIL_P(v)) {
- rb_raise(rb_eArgError, "invalid argument - %"PRIsVALUE, argv[0]);
- }
-
return v;
}
static VALUE
rand_random_number(int argc, VALUE *argv, VALUE obj)
{
- if (argc == 1 && argv[0] == INT2FIX(0)) --argc;
- return rand_random(argc, argv, obj, try_get_rnd(obj));
+ rb_random_t *rnd = try_get_rnd(obj);
+ VALUE v = rand_random(argc, argv, obj, rnd);
+ if (NIL_P(v)) v = rand_random(0, 0, obj, rnd);
+ return v;
}
/*
@@ -1350,7 +1357,9 @@ rb_f_rand(int argc, VALUE *argv, VALUE obj)
static VALUE
random_s_rand(int argc, VALUE *argv, VALUE obj)
{
- return rand_random(argc, argv, Qnil, rand_start(&default_rand));
+ VALUE v = rand_random(argc, argv, Qnil, rand_start(&default_rand));
+ if (NIL_P(v)) invalid_argument(argv[0]);
+ return v;
}
#define SIP_HASH_STREAMING 0