From a68406e84c28b2dc6e3cf8c5a0b8da25d69f98a8 Mon Sep 17 00:00:00 2001 From: djp952 Date: Mon, 8 Jul 2019 11:43:27 -0400 Subject: [PATCH] Use RtlGenRandom when available to elminiate TLS and heap leaks --- hdhomerun_os_windows.c | 26 ++++---------------------- hdhomerun_os_windows.h | 4 ++++ 2 files changed, 8 insertions(+), 22 deletions(-) diff --git a/hdhomerun_os_windows.c b/hdhomerun_os_windows.c index ea65945..ffc1d07 100644 --- a/hdhomerun_os_windows.c +++ b/hdhomerun_os_windows.c @@ -20,34 +20,16 @@ #include "hdhomerun.h" -#if defined(_WINRT) uint32_t random_get32(void) { - return (uint32_t)getcurrenttime(); -} -#else -uint32_t random_get32(void) -{ - static DWORD random_get32_context_tls = 0xFFFFFFFF; - if (random_get32_context_tls == 0xFFFFFFFF) { - random_get32_context_tls = TlsAlloc(); - } + uint32_t Result = (uint32_t)getcurrenttime(); - HCRYPTPROV *phProv = (HCRYPTPROV *)TlsGetValue(random_get32_context_tls); - if (!phProv) { - phProv = (HCRYPTPROV *)calloc(1, sizeof(HCRYPTPROV)); - CryptAcquireContext(phProv, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT); - TlsSetValue(random_get32_context_tls, phProv); - } - - uint32_t Result; - if (!CryptGenRandom(*phProv, sizeof(Result), (BYTE *)&Result)) { - return (uint32_t)getcurrenttime(); - } +#if defined(RtlGenRandom) + RtlGenRandom(&Result, sizeof(Result)); +#endif return Result; } -#endif uint64_t getcurrenttime(void) { diff --git a/hdhomerun_os_windows.h b/hdhomerun_os_windows.h index 62900e7..102e51f 100644 --- a/hdhomerun_os_windows.h +++ b/hdhomerun_os_windows.h @@ -46,6 +46,10 @@ #include #include +#define SystemFunction036 NTAPI SystemFunction036 +#include +#undef SystemFunction036 + #ifdef LIBHDHOMERUN_DLLEXPORT #define LIBHDHOMERUN_API __declspec(dllexport) #endif