diff options
| author | NAKAMURA Takumi <geek4civic@gmail.com> | 2012-05-06 08:24:24 +0000 |
|---|---|---|
| committer | NAKAMURA Takumi <geek4civic@gmail.com> | 2012-05-06 08:24:24 +0000 |
| commit | 7bec74112df4558fa7064c1dd3f993340b9d90bd (patch) | |
| tree | db933a6af7d36a5e1d97075896f65f284a05aa2d | |
| parent | 54acb28882bc42d58a0c0b8fdd8501bdb88df6e0 (diff) | |
| download | bcm5719-llvm-7bec74112df4558fa7064c1dd3f993340b9d90bd.tar.gz bcm5719-llvm-7bec74112df4558fa7064c1dd3f993340b9d90bd.zip | |
Unix/Process.inc: Give more useful random seed to srand. Workaround for PR12743.
llvm-svn: 156252
| -rw-r--r-- | llvm/lib/Support/Unix/Process.inc | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/llvm/lib/Support/Unix/Process.inc b/llvm/lib/Support/Unix/Process.inc index de982625e96..dd855b0d3ff 100644 --- a/llvm/lib/Support/Unix/Process.inc +++ b/llvm/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 } |

