[llvm-commits] [llvm] r156252 - /llvm/trunk/lib/Support/Unix/Process.inc

NAKAMURA Takumi geek4civic at gmail.com
Sun May 6 01:24:24 PDT 2012


Author: chapuni
Date: Sun May  6 03:24:24 2012
New Revision: 156252

URL: http://llvm.org/viewvc/llvm-project?rev=156252&view=rev
Log:
Unix/Process.inc: Give more useful random seed to srand. Workaround for PR12743.

Modified:
    llvm/trunk/lib/Support/Unix/Process.inc

Modified: llvm/trunk/lib/Support/Unix/Process.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Unix/Process.inc?rev=156252&r1=156251&r2=156252&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Unix/Process.inc (original)
+++ llvm/trunk/lib/Support/Unix/Process.inc Sun May  6 03:24:24 2012
@@ -298,11 +298,24 @@
   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
 }





More information about the llvm-commits mailing list