From 0157bcd8f8423a7dbbca02f282df0e3c090552dc Mon Sep 17 00:00:00 2001 From: "iposva@chromium.org" Date: Wed, 10 Sep 2008 06:59:41 +0000 Subject: [PATCH] Fix issue http://code.google.com/p/v8/issues/detail?id=58: - Prevent a clipping of values when converting a double to an unsigned int for use as the random generator's seed value. Review URL: http://codereview.chromium.org/1887 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@246 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/platform-linux.cc | 7 ++++++- src/platform-macos.cc | 7 ++++++- src/platform-win32.cc | 7 ++++++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/platform-linux.cc b/src/platform-linux.cc index 9bac309..ba72768 100644 --- a/src/platform-linux.cc +++ b/src/platform-linux.cc @@ -67,7 +67,12 @@ double ceiling(double x) { void OS::Setup() { // Seed the random number generator. - srandom(static_cast(TimeCurrentMillis())); + // Convert the current time to a 64-bit integer first, before converting it + // to an unsigned. Going directly can cause an overflow and the seed to be + // set to all ones. The seed will be identical for different instances that + // call this setup code within the same millisecond. + uint64_t seed = static_cast(TimeCurrentMillis()); + srandom(static_cast(seed)); } diff --git a/src/platform-macos.cc b/src/platform-macos.cc index 5edd7a6..7573125 100644 --- a/src/platform-macos.cc +++ b/src/platform-macos.cc @@ -73,7 +73,12 @@ double ceiling(double x) { void OS::Setup() { // Seed the random number generator. - srandom(static_cast(TimeCurrentMillis())); + // Convert the current time to a 64-bit integer first, before converting it + // to an unsigned. Going directly will cause an overflow and the seed to be + // set to all ones. The seed will be identical for different instances that + // call this setup code within the same millisecond. + uint64_t seed = static_cast(TimeCurrentMillis()); + srandom(static_cast(seed)); } diff --git a/src/platform-win32.cc b/src/platform-win32.cc index 2d54b6b..6035465 100644 --- a/src/platform-win32.cc +++ b/src/platform-win32.cc @@ -493,7 +493,12 @@ char* Time::LocalTimezone() { void OS::Setup() { // Seed the random number generator. - srand(static_cast(TimeCurrentMillis())); + // Convert the current time to a 64-bit integer first, before converting it + // to an unsigned. Going directly can cause an overflow and the seed to be + // set to all ones. The seed will be identical for different instances that + // call this setup code within the same millisecond. + uint64_t seed = static_cast(TimeCurrentMillis()); + srand(static_cast(seed)); } -- 2.7.4