From 95633841c67322066605494cdb5a9e9adc928580 Mon Sep 17 00:00:00 2001 From: Michael Ortmann <41313082+michaelortmann@users.noreply.github.com> Date: Thu, 4 Jul 2024 22:01:38 +0200 Subject: [PATCH] Applications should use the clock_gettime() function instead of the obsolescent gettimeofday() function. --- configure.ac | 2 +- src/main.c | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/configure.ac b/configure.ac index 9a5e1b7c2..5117d9a0e 100644 --- a/configure.ac +++ b/configure.ac @@ -112,7 +112,7 @@ AX_TYPE_SOCKLEN_T AX_CREATE_STDINT_H([eggint.h]) # Checks for functions and their arguments. -AC_CHECK_FUNCS([dprintf explicit_bzero memset_explicit explicit_memset getrandom inet_aton isascii memset_s snprintf strlcpy vsnprintf]) +AC_CHECK_FUNCS([clock_gettime dprintf explicit_bzero memset_explicit explicit_memset getrandom inet_aton isascii memset_s snprintf strlcpy vsnprintf]) AC_FUNC_SELECT_ARGTYPES EGG_FUNC_B64_NTOP AC_FUNC_MMAP diff --git a/src/main.c b/src/main.c index e78b18dd3..38e6a63f4 100644 --- a/src/main.c +++ b/src/main.c @@ -923,7 +923,7 @@ static void mainloop(int toplevel) static void init_random(void) { unsigned int seed; #ifdef HAVE_GETRANDOM - if (getrandom(&seed, sizeof(seed), 0) != sizeof(seed)) { + if (getrandom(&seed, sizeof seed, 0) != (sizeof seed)) { if (errno != ENOSYS) { fatal("ERROR: getrandom()\n", 0); } else { @@ -931,9 +931,15 @@ static void init_random(void) { * This can happen with glibc>=2.25 and linux<3.17 */ #endif +#ifdef HAVE_CLOCK_GETTIME + struct timespec tp; + clock_gettime(CLOCK_REALTIME, &tp); + seed = ((uint64_t) tp.tv_sec * tp.tv_nsec) ^ getpid(); +#else struct timeval tp; gettimeofday(&tp, NULL); - seed = (((int64_t) tp.tv_sec * tp.tv_usec)) ^ getpid(); + seed = ((uint64_t) tp.tv_sec * tp.tv_usec) ^ getpid(); +#endif #ifdef HAVE_GETRANDOM } }