[Cmake-commits] CMake branch, next, updated. v2.8.4-1557-g8961fdd
Brad King
brad.king at kitware.com
Tue May 17 11:20:59 EDT 2011
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "CMake".
The branch, next has been updated
via 8961fdddb5970b0f81db4e313247a13595f29ac7 (commit)
via e1b0a11dd471e1593ade56897185a9d4dd2e0857 (commit)
from 2c0ba4f3ca83554bd5bad403a2ecb02bafecfc43 (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
http://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=8961fdddb5970b0f81db4e313247a13595f29ac7
commit 8961fdddb5970b0f81db4e313247a13595f29ac7
Merge: 2c0ba4f e1b0a11
Author: Brad King <brad.king at kitware.com>
AuthorDate: Tue May 17 11:19:09 2011 -0400
Commit: CMake Topic Stage <kwrobot at kitware.com>
CommitDate: Tue May 17 11:19:09 2011 -0400
Merge topic 'string-RANDOM-seed' into next
e1b0a11 Improve string(RANDOM) default seed
http://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=e1b0a11dd471e1593ade56897185a9d4dd2e0857
commit e1b0a11dd471e1593ade56897185a9d4dd2e0857
Author: Brad King <brad.king at kitware.com>
AuthorDate: Tue May 17 10:07:26 2011 -0400
Commit: Brad King <brad.king at kitware.com>
CommitDate: Tue May 17 10:07:26 2011 -0400
Improve string(RANDOM) default seed
The naive time(0) seed is unique only within one second. Instead try to
read a real source of entropy and otherwise fall back to a combination
of the process id and high-resolution time.
diff --git a/Source/cmStringCommand.cxx b/Source/cmStringCommand.cxx
index 19d2369..e3bf08f 100644
--- a/Source/cmStringCommand.cxx
+++ b/Source/cmStringCommand.cxx
@@ -770,7 +770,7 @@ bool cmStringCommand
static bool seeded = false;
bool force_seed = false;
- int seed = (int) time(NULL);
+ int seed = 0;
int length = 5;
const char cmStringCommandDefaultAlphabet[] = "qwertyuiopasdfghjklzxcvbnm"
"QWERTYUIOPASDFGHJKLZXCVBNM"
@@ -825,7 +825,7 @@ bool cmStringCommand
if (!seeded || force_seed)
{
seeded = true;
- srand(seed);
+ srand(force_seed? seed : cmSystemTools::RandomSeed());
}
const char* alphaPtr = alphabet.c_str();
diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx
index 7bc89a4..4167355 100644
--- a/Source/cmSystemTools.cxx
+++ b/Source/cmSystemTools.cxx
@@ -31,7 +31,9 @@
#if defined(_WIN32)
# include <windows.h>
+# include <wincrypt.h>
#else
+# include <sys/time.h>
# include <sys/types.h>
# include <unistd.h>
# include <utime.h>
@@ -2233,6 +2235,68 @@ bool cmSystemTools::FileTimeSet(const char* fname, cmSystemToolsFileTime* t)
}
//----------------------------------------------------------------------------
+#ifdef _WIN32
+static int WinCryptRandom(void* data, size_t size)
+{
+ int result = 0;
+ HCRYPTPROV hProvider = 0;
+ if(CryptAcquireContextW(&hProvider, 0, 0, PROV_RSA_FULL,
+ CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
+ {
+ result = CryptGenRandom(hProvider, (DWORD)size, (BYTE*)data)? 1:0;
+ CryptReleaseContext(hProvider, 0);
+ }
+ return result;
+}
+#endif
+
+//----------------------------------------------------------------------------
+unsigned int cmSystemTools::RandomSeed()
+{
+#if defined(_WIN32) && !defined(__CYGWIN__)
+ unsigned int seed = 0;
+
+ // Try using a real random source.
+ if(WinCryptRandom(&seed, sizeof(seed)))
+ {
+ return seed;
+ }
+
+ // Fall back to the time and pid.
+ FILETIME ft;
+ GetSystemTimeAsFileTime(&ft);
+ unsigned int t1 = static_cast<unsigned int>(ft.dwHighDateTime);
+ unsigned int t2 = static_cast<unsigned int>(ft.dwLowDateTime);
+ unsigned int pid = static_cast<unsigned int>(GetCurrentProcessId());
+ return t1 ^ t2 ^ pid;
+#else
+ union
+ {
+ unsigned int integer;
+ char bytes[sizeof(unsigned int)];
+ } seed;
+
+ // Try using a real random source.
+ std::ifstream fin("/dev/urandom");
+ if(fin && fin.read(seed.bytes, sizeof(seed)) &&
+ fin.gcount() == sizeof(seed))
+ {
+ return seed.integer;
+ }
+
+ // Fall back to the time and pid.
+ struct timeval t;
+ gettimeofday(&t, 0);
+ unsigned int pid = static_cast<unsigned int>(getpid());
+ unsigned int tv_sec = t.tv_sec;
+ unsigned int tv_usec = t.tv_usec;
+ // Since tv_usec never fills more than 11 bits we shift it to fill
+ // in the slow-changing high-order bits of tv_sec.
+ return tv_sec ^ (tv_usec << 21) ^ pid;
+#endif
+}
+
+//----------------------------------------------------------------------------
static std::string cmSystemToolsExecutableDirectory;
void cmSystemTools::FindExecutableDirectory(const char* argv0)
{
diff --git a/Source/cmSystemTools.h b/Source/cmSystemTools.h
index 6f9147c..78b9abf 100644
--- a/Source/cmSystemTools.h
+++ b/Source/cmSystemTools.h
@@ -402,6 +402,9 @@ public:
static bool FileTimeGet(const char* fname, cmSystemToolsFileTime* t);
static bool FileTimeSet(const char* fname, cmSystemToolsFileTime* t);
+ /** Random seed generation. */
+ static unsigned int RandomSeed();
+
/** Find the directory containing the running executable. Save it
in a global location to be queried by GetExecutableDirectory
later. */
-----------------------------------------------------------------------
Summary of changes:
Source/cmStringCommand.cxx | 4 +-
Source/cmSystemTools.cxx | 64 ++++++++++++++++++++++++++++++++++++++++++++
Source/cmSystemTools.h | 3 ++
3 files changed, 69 insertions(+), 2 deletions(-)
hooks/post-receive
--
CMake
More information about the Cmake-commits
mailing list