On Jul 13, 2014, at 3:58 PM, Ted Unangst <t...@tedunangst.com> wrote:

> If the user calls srandomdev(), they are asking for an unpredictable
> sequence, even one that could not normally be produced. So give them
> one. Use arc4random in that case.
> 
> 
> Index: stdlib/random.c
> ===================================================================
> RCS file: /cvs/src/lib/libc/stdlib/random.c,v
> retrieving revision 1.22
> diff -u -p -r1.22 random.c
> --- stdlib/random.c   15 Jun 2014 05:10:58 -0000      1.22
> +++ stdlib/random.c   13 Jul 2014 13:56:34 -0000
> @@ -176,6 +176,8 @@ static int rand_type = TYPE_3;
> static int rand_deg = DEG_3;
> static int rand_sep = SEP_3;
> 
> +static int use_arc4random;
> +
> _THREAD_PRIVATE_MUTEX(random);
> static long random_l(void);
> 
> @@ -201,6 +203,7 @@ srandom_l(unsigned int x)
>       int32_t test;
>       div_t val;
> 
> +     use_arc4random = 0;
>       if (rand_type == TYPE_0)
>               state[0] = x;
>       else {
> @@ -254,17 +257,7 @@ srandomdev(void)
>       size_t len;
> 
>       LOCK();
> -     if (rand_type == TYPE_0)
> -             len = sizeof(state[0]);
> -     else
> -             len = rand_deg * sizeof(state[0]);
> -
> -     arc4random_buf(state, len);
> -
> -     if (rand_type != TYPE_0) {
> -             fptr = &state[rand_sep];
> -             rptr = &state[0];
> -     }
> +     use_arc4random = 1;
>       UNLOCK();
> }
> 
> @@ -411,6 +404,9 @@ static long
> random_l(void)
> {
>       int32_t i;
> +
> +     if (use_arc4random)
> +             return arc4random() & 0x7fffffff;

return arc4random() % ((unsigned)RAND_MAX + 1) ?

> 
>       if (rand_type == TYPE_0)
>               i = state[0] = (state[0] * 1103515245 + 12345) & 0x7fffffff;
> 

Reply via email to