diff options
author | Nicholas Marriott <nicholas.marriott@gmail.com> | 2012-04-09 10:46:32 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2012-04-09 10:46:32 -0400 |
commit | e86af4b7e56ed5b7050cb4f41ae534f54748598c (patch) | |
tree | 8ca8da23c7ce08b9f803c87067246c283920aeb7 /test/regress_buffer.c | |
parent | d9a55153366a6b842761e380d65b13fd70d9a507 (diff) | |
download | libevent-e86af4b7e56ed5b7050cb4f41ae534f54748598c.tar.gz |
Change evutil_weakrand_() to avoid platform random()
This change allows us to avoid perturbing the platform's random(), and
to avoid hitting locks on random() in the platform's libc.
evutil_weakrand_() is, well, weak, so we choose here an algorithm that
favors speed over a number of other possibly desirable properties.
We're using a linear congruential generator, and taking our parameters
from those shared by the OpenBSD random() implementation, and
Glibc's fastest random() implementation.
The low bits of a LCG of modulus 2^32 are (notoriously) less random
than the higher bits. So to generate a random value in a range, using
the % operator is no good; we ought to divide. We add an
evutil_weakrand_range_() function to do that.
This code also changes the interface of evutil_weakrand_() so that it
now manipulates an explicit seed, rather than having the seed in a
static variable. This change enables us to use existing locks to
achieve thread-safety, rather than having to rely on an additional lock.
(Patch by Nicholas Marriott; commit message by Nick Mathewson.)
Diffstat (limited to 'test/regress_buffer.c')
-rw-r--r-- | test/regress_buffer.c | 3 |
1 files changed, 2 insertions, 1 deletions
diff --git a/test/regress_buffer.c b/test/regress_buffer.c index dfb680b5..2e4ef176 100644 --- a/test/regress_buffer.c +++ b/test/regress_buffer.c @@ -699,6 +699,7 @@ test_evbuffer_add_file(void *ptr) struct event *rev=NULL, *wev=NULL; struct event_base *base = testdata->base; evutil_socket_t pair[2] = {-1, -1}; + static ev_uint32_t seed = 123456789U; /* This test is highly parameterized based on substrings of its * argument. The strings are: */ @@ -757,7 +758,7 @@ test_evbuffer_add_file(void *ptr) data = malloc(1024*512); tt_assert(data); for (i = 0; i < datalen; ++i) - data[i] = (char)evutil_weakrand_(); + data[i] = (char)evutil_weakrand_(&seed); } else { data = strdup("here is a relatively small string."); tt_assert(data); |