comparison src/c/urweb.c @ 1933:8e6e3d9cea22

RAND_bytes isn't thread-safe, so wrap it with a lock
author Adam Chlipala <adam@chlipala.net>
date Wed, 11 Dec 2013 18:54:42 -0500
parents 98895243b5b6
children 420d129c4174
comparison
equal deleted inserted replaced
1932:98895243b5b6 1933:8e6e3d9cea22
4021 else 4021 else
4022 fprintf(stderr, "%s\n", s); 4022 fprintf(stderr, "%s\n", s);
4023 return uw_unit_v; 4023 return uw_unit_v;
4024 } 4024 }
4025 4025
4026 static pthread_mutex_t rand_mutex = PTHREAD_MUTEX_INITIALIZER;
4027
4026 uw_Basis_int uw_Basis_rand(uw_context ctx) { 4028 uw_Basis_int uw_Basis_rand(uw_context ctx) {
4027 uw_Basis_int ret; 4029 uw_Basis_int ret;
4030 pthread_mutex_lock(&rand_mutex);
4028 int r = RAND_bytes((unsigned char *)&ret, sizeof ret); 4031 int r = RAND_bytes((unsigned char *)&ret, sizeof ret);
4032 pthread_mutex_unlock(&rand_mutex);
4029 4033
4030 if (r) 4034 if (r)
4031 return abs(ret); 4035 return abs(ret);
4032 else 4036 else
4033 uw_error(ctx, FATAL, "Random number generation failed"); 4037 uw_error(ctx, FATAL, "Random number generation failed");