summaryrefslogtreecommitdiff
path: root/random.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-02-14 05:40:10 (GMT)
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-02-14 05:40:10 (GMT)
commit10a9807030020432e92008615dde14fc657b17ff (patch)
treea4b1c8322d7f3ea4a4e4a75cac62f3ce5a21fb68 /random.c
parentdd09b5bf1d0b45f6d74df49ad1deea9762036b10 (diff)
random.c: ArgumentError for invalid argument
* random.c (rand_random_number): raise ArgumentError for invalid argument like as SecureRandom.random_number. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49599 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'random.c')
-rw-r--r--random.c21
1 files changed, 17 insertions, 4 deletions
diff --git a/random.c b/random.c
index a48573a..4734cf7 100644
--- a/random.c
+++ b/random.c
@@ -1077,6 +1077,19 @@ invalid_argument(VALUE arg0)
rb_raise(rb_eArgError, "invalid argument - %"PRIsVALUE, arg0);
}
+static VALUE
+check_random_number(VALUE v, const VALUE *argv)
+{
+ switch (v) {
+ case Qfalse:
+ (void)NUM2LONG(argv[0]);
+ break;
+ case Qnil:
+ invalid_argument(argv[0]);
+ }
+ return v;
+}
+
static inline double
float_value(VALUE v)
{
@@ -1198,7 +1211,7 @@ static VALUE
random_rand(int argc, VALUE *argv, VALUE obj)
{
VALUE v = rand_random(argc, argv, obj, get_rnd(obj));
- if (NIL_P(v)) invalid_argument(argv[0]);
+ check_random_number(v, argv);
return v;
}
@@ -1242,8 +1255,7 @@ rand_random(int argc, VALUE *argv, VALUE obj, rb_random_t *rnd)
/* nothing to do */
}
else {
- v = Qnil;
- (void)NUM2LONG(vmax);
+ return Qfalse;
}
return v;
}
@@ -1254,6 +1266,7 @@ rand_random_number(int argc, VALUE *argv, VALUE 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);
+ else if (!v) invalid_argument(argv[0]);
return v;
}
@@ -1358,7 +1371,7 @@ static VALUE
random_s_rand(int argc, VALUE *argv, VALUE obj)
{
VALUE v = rand_random(argc, argv, Qnil, rand_start(&default_rand));
- if (NIL_P(v)) invalid_argument(argv[0]);
+ check_random_number(v, argv);
return v;
}