From: Aki Tuomi Date: Wed, 23 Aug 2017 09:38:07 +0000 (+0300) Subject: lib: Replace rand.c with i_rand function X-Git-Tag: 2.3.0.rc1~1082 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=5405ee4d314ccb4a9c3c0c20f3d9996a9eba5ee2;p=thirdparty%2Fdovecot%2Fcore.git lib: Replace rand.c with i_rand function Replacement for rand --- diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 8791f1fc62..a018b7c5eb 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -269,7 +269,6 @@ headers = \ printf-format-fix.h \ process-title.h \ priorityq.h \ - rand.h \ randgen.h \ read-full.h \ restrict-access.h \ diff --git a/src/lib/lib.c b/src/lib/lib.c index 7a987ea098..4f998ba07d 100644 --- a/src/lib/lib.c +++ b/src/lib/lib.c @@ -9,6 +9,7 @@ #include "ipwd.h" #include "process-title.h" #include "var-expand-private.h" +#include "randgen.h" #include #include @@ -163,14 +164,8 @@ static void lib_open_non_stdio_dev_null(void) void lib_init(void) { - struct timeval tv; i_assert(!lib_initialized); - - /* standard way to get rand() return different values. */ - if (gettimeofday(&tv, NULL) < 0) - i_fatal("gettimeofday(): %m"); - srand((unsigned int) (tv.tv_sec ^ tv.tv_usec ^ getpid())); - + random_init(); data_stack_init(); hostpid_init(); lib_open_non_stdio_dev_null(); @@ -197,4 +192,5 @@ void lib_deinit(void) env_deinit(); failures_deinit(); process_title_deinit(); + random_deinit(); } diff --git a/src/lib/lib.h b/src/lib/lib.h index 528165829f..e795101e4d 100644 --- a/src/lib/lib.h +++ b/src/lib/lib.h @@ -32,7 +32,6 @@ #include "mempool.h" #include "imem.h" #include "byteorder.h" -#include "rand.h" typedef struct buffer buffer_t; typedef struct buffer string_t; @@ -90,4 +89,6 @@ void lib_init(void); bool lib_is_initialized(void); void lib_deinit(void); +uint32_t i_rand(void); + #endif diff --git a/src/lib/rand.c b/src/lib/rand.c index ec4930594a..781a38f6bc 100644 --- a/src/lib/rand.c +++ b/src/lib/rand.c @@ -1,14 +1,22 @@ /* Copyright (c) 2014-2017 Dovecot authors, see the included COPYING file */ #include "lib.h" +#include "randgen.h" #ifdef HAVE_ARC4RANDOM #ifdef HAVE_LIBBSD #include #endif -/* this returns [0,RAND_MAX), to keep it compatible with rand() */ -int arc4random_rand(void) { - return (int)(arc4random() % ((unsigned)RAND_MAX + 1)); +uint32_t i_rand(void) +{ + return arc4random(); +} +#else +uint32_t i_rand(void) +{ + uint32_t value; + random_fill(&value, sizeof(value)); + return value; } #endif diff --git a/src/lib/rand.h b/src/lib/rand.h deleted file mode 100644 index cf3d8c2ca0..0000000000 --- a/src/lib/rand.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef RAND_H -#define RAND_H - -#ifdef HAVE_ARC4RANDOM - -int arc4random_rand(void); -#define rand arc4random_rand - -#endif - -#endif