aboutsummaryrefslogtreecommitdiff
path: root/lib/Support
diff options
context:
space:
mode:
authorNAKAMURA Takumi <geek4civic@gmail.com>2012-05-06 08:24:24 +0000
committerNAKAMURA Takumi <geek4civic@gmail.com>2012-05-06 08:24:24 +0000
commitdc736b0a3e02f2470d7c580e1f3c2d4654170597 (patch)
tree7bec573da71e98e449d42e0e4ae73e79b9ce7c24 /lib/Support
parent24cddd5c9a7622e06e7ae0e0887e5e0d48326606 (diff)
Unix/Process.inc: Give more useful random seed to srand. Workaround for PR12743.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@156252 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Support')
-rw-r--r--lib/Support/Unix/Process.inc15
1 files changed, 14 insertions, 1 deletions
diff --git a/lib/Support/Unix/Process.inc b/lib/Support/Unix/Process.inc
index de982625e9..dd855b0d3f 100644
--- a/lib/Support/Unix/Process.inc
+++ b/lib/Support/Unix/Process.inc
@@ -298,11 +298,24 @@ const char *Process::ResetColor() {
return "\033[0m";
}
+#if !defined(HAVE_ARC4RANDOM)
+static unsigned GetRandomNumberSeed() {
+ unsigned seed = ::time(NULL); // FIXME: It might not provide unique seed.
+ FILE *RandomSource = ::fopen("/dev/urandom", "r");
+ if (RandomSource) {
+ ::fread((void *)&seed, sizeof(seed), 1, RandomSource);
+ ::fclose(RandomSource);
+ }
+ return seed;
+}
+#endif
+
unsigned llvm::sys::Process::GetRandomNumber() {
#if defined(HAVE_ARC4RANDOM)
return arc4random();
#else
- static int x = (::srand(::time(NULL)), 0);
+ static int x = (::srand(GetRandomNumberSeed()), 0);
+ (void)x;
return ::rand();
#endif
}