From 4fbe44605bbb60507554e1c4e3e6baf44858dbea Mon Sep 17 00:00:00 2001 From: nmlgc Date: Sat, 15 Feb 2014 17:10:24 +0100 Subject: [PATCH] Three fixes for hashtable seeding on Windows First, wrap "advapi32.dll" into the TEXT() macro. If UNICODE is defined, GetModuleHandle() redirects to GetModuleHandleW(), which excepts a wchar_t* UTF-16 parameter, thus causing a compile error. TEXT() prefixes the string literal with L in this case, and does nothing otherwise. Second, make sure that CryptGenRandom() is actually called through the function pointer retrieved by the call to GetProcAddress() above. And third, replace _getpid() with the equivalent and more ubiquitous Win32 API function GetCurrentProcessId(). Since _getpid() is not exported by all C runtimes on Windows (most notably the Driver Development Kit), using it might introduce previously unneeded runtime dependencies. GetCurrentProcessId(), on the other hand, has been available in kernel32.dll since at least Windows 95, just like the other API functions used in this code (GetModuleHandle() and GetProcAddress()). --- src/hashtable_seed.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/hashtable_seed.c b/src/hashtable_seed.c index b6f9ead..751e0e3 100644 --- a/src/hashtable_seed.c +++ b/src/hashtable_seed.c @@ -38,8 +38,8 @@ #endif #if defined(_WIN32) -/* For _getpid() */ -#include +/* For GetModuleHandle(), GetProcAddress() and GetCurrentProcessId() */ +#include #endif #include "jansson.h" @@ -95,7 +95,6 @@ static int seed_from_urandom(uint32_t *seed) { /* Windows Crypto API */ #if defined(_WIN32) && defined(USE_WINDOWS_CRYPTOAPI) -#include #include typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv, LPCSTR pszContainer, LPCSTR pszProvider, DWORD dwProvType, DWORD dwFlags); @@ -112,7 +111,7 @@ static int seed_from_windows_cryptoapi(uint32_t *seed) BYTE data[sizeof(uint32_t)]; int ok; - hAdvAPI32 = GetModuleHandle("advapi32.dll"); + hAdvAPI32 = GetModuleHandle(TEXT("advapi32.dll")); if(hAdvAPI32 == NULL) return 1; @@ -131,7 +130,7 @@ static int seed_from_windows_cryptoapi(uint32_t *seed) if (!pCryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) return 1; - ok = CryptGenRandom(hCryptProv, sizeof(uint32_t), data); + ok = pCryptGenRandom(hCryptProv, sizeof(uint32_t), data); pCryptReleaseContext(hCryptProv, 0); if (!ok) @@ -156,7 +155,7 @@ static int seed_from_timestamp_and_pid(uint32_t *seed) { /* XOR with PID for more randomness */ #if defined(_WIN32) - *seed ^= (uint32_t)_getpid(); + *seed ^= (uint32_t)GetCurrentProcessId(); #elif defined(HAVE_GETPID) *seed ^= (uint32_t)getpid(); #endif