summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/Support/Process.cpp5
-rw-r--r--lib/Support/Windows/Process.inc7
-rw-r--r--unittests/Support/ProcessTest.cpp7
3 files changed, 19 insertions, 0 deletions
diff --git a/lib/Support/Process.cpp b/lib/Support/Process.cpp
index d5168f03a6..1360842753 100644
--- a/lib/Support/Process.cpp
+++ b/lib/Support/Process.cpp
@@ -12,6 +12,11 @@
//===----------------------------------------------------------------------===//
#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"
diff --git a/lib/Support/Windows/Process.inc b/lib/Support/Windows/Process.inc
index 750097eecf..16e4092e9f 100644
--- a/lib/Support/Windows/Process.inc
+++ b/lib/Support/Windows/Process.inc
@@ -360,3 +360,10 @@ const char *Process::ResetColor() {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), defaultColors());
return 0;
}
+
+unsigned Process::GetRandomNumber() {
+ unsigned int result;
+ const errno_t ec = rand_s(&result);
+ assert(ec == 0 && "rand_s failed");
+ return result;
+}
diff --git a/unittests/Support/ProcessTest.cpp b/unittests/Support/ProcessTest.cpp
index af6a6f921b..19694e55d0 100644
--- a/unittests/Support/ProcessTest.cpp
+++ b/unittests/Support/ProcessTest.cpp
@@ -39,6 +39,13 @@ TEST(ProcessTest, SelfProcess) {
EXPECT_GT(TimeValue::MaxTime, process::get_self()->get_wall_time());
}
+TEST(ProcessTest, GetRandomNumberTest) {
+ const unsigned r1 = Process::GetRandomNumber();
+ const unsigned r2 = Process::GetRandomNumber();
+ // It should be extremely unlikely that both r1 and r2 are 0.
+ EXPECT_NE((r1 | r2), 0);
+}
+
#ifdef _MSC_VER
#define setenv(name, var, ignore) _putenv_s(name, var)
#endif