diff options
author | Adam Chlipala <adam@chlipala.net> | 2015-01-12 12:02:54 -0500 |
---|---|---|
committer | Adam Chlipala <adam@chlipala.net> | 2015-01-12 12:02:54 -0500 |
commit | 9e3b2195e8a9bb069ea7109249662ef033c488bc (patch) | |
tree | 3ad0a78a5e031c32e32ebd3c046c365fdb2e23f8 /src | |
parent | 20a175809da5adf75908e0d6fef51738e6d0363c (diff) |
Switch to using OpenSSL PRNG for the one remaining rand()
Diffstat (limited to 'src')
-rw-r--r-- | src/c/urweb.c | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/src/c/urweb.c b/src/c/urweb.c index e2881b05..4a00755b 100644 --- a/src/c/urweb.c +++ b/src/c/urweb.c @@ -167,6 +167,19 @@ void *uw_init_client_data(); void uw_free_client_data(void *); void uw_copy_client_data(void *dst, void *src); +static pthread_mutex_t rand_mutex = PTHREAD_MUTEX_INITIALIZER; + +static uw_Basis_int my_rand() { + pthread_mutex_lock(&rand_mutex); + int r = RAND_bytes((unsigned char *)&ret, sizeof ret); + pthread_mutex_unlock(&rand_mutex); + + if (r) + return abs(r); + else + return -1; +} + static client *new_client() { client *c; @@ -192,7 +205,7 @@ static client *new_client() { pthread_mutex_lock(&c->lock); c->mode = USED; - c->pass = rand(); + c->pass = my_rand(); c->sock = -1; c->last_contact = time(NULL); uw_buffer_reset(&c->msgs); @@ -4221,16 +4234,11 @@ uw_Basis_unit uw_Basis_debug(uw_context ctx, uw_Basis_string s) { return uw_unit_v; } -static pthread_mutex_t rand_mutex = PTHREAD_MUTEX_INITIALIZER; - uw_Basis_int uw_Basis_rand(uw_context ctx) { - uw_Basis_int ret; - pthread_mutex_lock(&rand_mutex); - int r = RAND_bytes((unsigned char *)&ret, sizeof ret); - pthread_mutex_unlock(&rand_mutex); + int r = my_rand(); - if (r) - return abs(ret); + if (r >= 0) + return r; else uw_error(ctx, FATAL, "Random number generation failed"); } |