summaryrefslogtreecommitdiff
path: root/random.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-05-13 15:16:57 (GMT)
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-05-13 15:16:57 (GMT)
commitd009cebf0faff38a99c7fcb47b08b052303cf3db (patch)
treec34fdd596615473c96b754057c3e3a9d468ec7b8 /random.c
parent00a371c28d1cd964efc60dfe9da1ff59b2096175 (diff)
random.c: no local copy of the seed
* random.c (make_seed_value): append leading-zero-guard and get rid of making a local copy of the seed. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54997 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'random.c')
-rw-r--r--random.c22
1 files changed, 7 insertions, 15 deletions
diff --git a/random.c b/random.c
index 64952ba..1770adf 100644
--- a/random.c
+++ b/random.c
@@ -567,21 +567,13 @@ fill_random_seed(uint32_t *seed, size_t cnt)
}
static VALUE
-make_seed_value(const uint32_t *ptr)
+make_seed_value(uint32_t *ptr, size_t len)
{
VALUE seed;
- size_t len;
- uint32_t buf[DEFAULT_SEED_CNT+1];
- if (ptr[DEFAULT_SEED_CNT-1] <= 1) {
+ if (ptr[len-1] <= 1) {
/* set leading-zero-guard */
- MEMCPY(buf, ptr, uint32_t, DEFAULT_SEED_CNT);
- buf[DEFAULT_SEED_CNT] = 1;
- ptr = buf;
- len = DEFAULT_SEED_CNT+1;
- }
- else {
- len = DEFAULT_SEED_CNT;
+ ptr[len++] = 1;
}
seed = rb_integer_unpack(ptr, len, sizeof(uint32_t), 0,
@@ -602,9 +594,9 @@ static VALUE
random_seed(void)
{
VALUE v;
- uint32_t buf[DEFAULT_SEED_CNT];
+ uint32_t buf[DEFAULT_SEED_CNT+1];
fill_random_seed(buf, DEFAULT_SEED_CNT);
- v = make_seed_value(buf);
+ v = make_seed_value(buf, DEFAULT_SEED_CNT);
explicit_bzero(buf, DEFAULT_SEED_LEN);
return v;
}
@@ -1553,12 +1545,12 @@ Init_RandomSeedCore(void)
static VALUE
init_randomseed(struct MT *mt)
{
- uint32_t initial[DEFAULT_SEED_CNT];
+ uint32_t initial[DEFAULT_SEED_CNT+1];
VALUE seed;
fill_random_seed(initial, DEFAULT_SEED_CNT);
init_by_array(mt, initial, DEFAULT_SEED_CNT);
- seed = make_seed_value(initial);
+ seed = make_seed_value(initial, DEFAULT_SEED_CNT);
explicit_bzero(initial, DEFAULT_SEED_LEN);
return seed;
}