From 9b5a6fe1dc9ab40996b71e87763933304a706507 Mon Sep 17 00:00:00 2001 From: Simon Goldschmidt Date: Mon, 17 Feb 2020 21:28:04 +0100 Subject: [PATCH] win32 port: fix LWIP_RAND() being called without sys_init() This can happen in tests... --- contrib/ports/win32/sys_arch.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/contrib/ports/win32/sys_arch.c b/contrib/ports/win32/sys_arch.c index b08c63b9..94806b22 100644 --- a/contrib/ports/win32/sys_arch.c +++ b/contrib/ports/win32/sys_arch.c @@ -78,17 +78,6 @@ static DWORD netconn_sem_tls_index; static HCRYPTPROV hcrypt; -unsigned int -lwip_port_rand(void) -{ - u32_t ret; - if (CryptGenRandom(hcrypt, sizeof(ret), (BYTE*)&ret)) { - return ret; - } - LWIP_ASSERT("CryptGenRandom failed", 0); - return 0; -} - static void sys_win_rand_init(void) { @@ -105,6 +94,22 @@ sys_win_rand_init(void) } } +unsigned int +lwip_port_rand(void) +{ + u32_t ret; + if (CryptGenRandom(hcrypt, sizeof(ret), (BYTE*)&ret)) { + return ret; + } + // maybe CryptAcquireContext has not been called... + sys_win_rand_init(); + if (CryptGenRandom(hcrypt, sizeof(ret), (BYTE*)&ret)) { + return ret; + } + LWIP_ASSERT("CryptGenRandom failed", 0); + return 0; +} + static void sys_init_timing(void) {