Hopefully fixing the MinGW 32 build, which was broken by r200767. Not using rand_s...
authorAaron Ballman <aaron@aaronballman.com>
Tue, 11 Feb 2014 02:47:33 +0000 (02:47 +0000)
committerAaron Ballman <aaron@aaronballman.com>
Tue, 11 Feb 2014 02:47:33 +0000 (02:47 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@201124 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Support/Process.cpp
lib/Support/Windows/Process.inc

index 1360842753d078e118f3a76ea6ef18054bc9b2eb..d5168f03a6de0686c0b0692b7b84b34c8b01fdb3 100644 (file)
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Config/config.h"
-#if LLVM_ON_WIN32
-  // This define makes stdlib.h declare the rand_s function.
-#define _CRT_RAND_S
-#include <stdlib.h>
-#endif
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/Process.h"
 
index 62b6da0cb1bee8f4bab60993da30cc1b544692c5..a0e3bc413a3f2c32d9de0fc28fafeefc5b1337c7 100644 (file)
@@ -362,8 +362,15 @@ const char *Process::ResetColor() {
 }
 
 unsigned Process::GetRandomNumber() {
-  unsigned int result;
-  const int ec = rand_s(&result);
-  assert(ec == 0 && "rand_s failed");
-  return result;
+  HCRYPTPROV HCPC;
+  if (!::CryptAcquireContextW(&HCPC, NULL, NULL, PROV_RSA_FULL,
+                              CRYPT_VERIFYCONTEXT))
+    assert(false && "Could not acquire a cryptographic context");
+
+  ScopedCryptContext CryptoProvider(HCPC);
+  unsigned Ret;
+  if (!::CryptGenRandom(CryptoProvider, sizeof(Ret),
+                        reinterpret_cast<BYTE *>(&Ret)))
+    assert(false && "Could not generate a random number");
+  return Ret;
 }