# HG changeset patch # User Adam Chlipala # Date 1421082174 18000 # Node ID 7755f00a4fc3dc565f8a52bbd4665f9018ab77d3 # Parent 661b531f55bde2fb10a4a59d4b9095e19e99566a Switch to using OpenSSL PRNG for the one remaining rand() diff -r 661b531f55bd -r 7755f00a4fc3 src/c/urweb.c --- a/src/c/urweb.c Wed Jan 07 09:25:13 2015 -0500 +++ b/src/c/urweb.c Mon Jan 12 12:02:54 2015 -0500 @@ -167,6 +167,19 @@ 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 @@ 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 @@ 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); - - if (r) - return abs(ret); + int r = my_rand(); + + if (r >= 0) + return r; else uw_error(ctx, FATAL, "Random number generation failed"); }