From: Aki Tuomi Date: Mon, 4 Sep 2017 12:22:13 +0000 (+0300) Subject: lib: Replace rand_set_seed with srand X-Git-Tag: 2.3.0.rc1~1083 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a147c9a2092a1b54bc5441fbaa43c56458cfd827;p=thirdparty%2Fdovecot%2Fcore.git lib: Replace rand_set_seed with srand Makes following commits easier, and also makes sure that rand() will get seeded when used by other libraries. --- diff --git a/src/lib/lib.c b/src/lib/lib.c index 4c20163d36..7a987ea098 100644 --- a/src/lib/lib.c +++ b/src/lib/lib.c @@ -169,7 +169,7 @@ void lib_init(void) /* standard way to get rand() return different values. */ if (gettimeofday(&tv, NULL) < 0) i_fatal("gettimeofday(): %m"); - rand_set_seed((unsigned int) (tv.tv_sec ^ tv.tv_usec ^ getpid())); + srand((unsigned int) (tv.tv_sec ^ tv.tv_usec ^ getpid())); data_stack_init(); hostpid_init(); diff --git a/src/lib/rand.c b/src/lib/rand.c index 9c66c7cc37..ec4930594a 100644 --- a/src/lib/rand.c +++ b/src/lib/rand.c @@ -1,37 +1,7 @@ /* Copyright (c) 2014-2017 Dovecot authors, see the included COPYING file */ -/* Wrap srand() so that we can reproduce fuzzed tests */ - #include "lib.h" -static int seeded = 0; -static unsigned int seed; -static char const *env_seed; - -int rand_get_seed_count(void) -{ - return seeded; -} -unsigned int rand_get_last_seed(void) -{ - i_assert(seeded > 0); - return seed; -} -void rand_set_seed(unsigned int s) -{ - if (seeded == 0) { - unsigned int seedval; - env_seed = getenv("DOVECOT_SRAND"); - if (env_seed != NULL && str_to_uint(env_seed, &seedval) >= 0) - seed = seedval; - } - seeded++; - if (env_seed == NULL) - seed = s; - - srand(seed); -} - #ifdef HAVE_ARC4RANDOM #ifdef HAVE_LIBBSD #include diff --git a/src/lib/rand.h b/src/lib/rand.h index b0de58f514..cf3d8c2ca0 100644 --- a/src/lib/rand.h +++ b/src/lib/rand.h @@ -1,21 +1,6 @@ #ifndef RAND_H #define RAND_H -/* Wrap srand() so that we can reproduce fuzzed tests */ - -/* If we have seeded the prng precisely once, and we remember what - * value that was with, then we can reproduce any failing test cases - * that depend on that randomness by forcing the seed value (e.g. - * in a debugger, by putting a breakpoint on rand_set_seed()). - */ - -/* Number of times we've been seeded */ -int rand_get_seed_count(void); -/* That last seed */ -unsigned int rand_get_last_seed(void); -/* Actually seed the prng (could add char* for name of function?) */ -void rand_set_seed(unsigned int s); - #ifdef HAVE_ARC4RANDOM int arc4random_rand(void); diff --git a/src/lib/randgen.c b/src/lib/randgen.c index 0bcbe6407d..332fc84597 100644 --- a/src/lib/randgen.c +++ b/src/lib/randgen.c @@ -48,7 +48,7 @@ void random_init(void) } random_fill(&seed, sizeof(seed)); - rand_set_seed(seed); + srand(seed); fd_close_on_exec(urandom_fd, TRUE); }