From d009cebf0faff38a99c7fcb47b08b052303cf3db Mon Sep 17 00:00:00 2001 From: nobu Date: Fri, 13 May 2016 15:16:57 +0000 Subject: 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 --- ChangeLog | 5 +++++ random.c | 22 +++++++--------------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/ChangeLog b/ChangeLog index 4e3d13d297..08e62a2a52 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Sat May 14 00:16:54 2016 Nobuyoshi Nakada + + * random.c (make_seed_value): append leading-zero-guard and get + rid of making a local copy of the seed. + Fri May 13 08:46:42 2016 cremno * NEWS: drop FreeBSD < 4 support. diff --git a/random.c b/random.c index 64952ba14c..1770adf138 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; } -- cgit v1.2.3