From 3f6247914f665a141bec4a5091b1935a22ac94ef Mon Sep 17 00:00:00 2001 From: Dwayne Boone Date: Tue, 8 Sep 2009 15:56:07 -0230 Subject: Bug 3459 - use rand_s on windows for random number generation --- cpp/src/IceUtil/Random.cpp | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) (limited to 'cpp/src/IceUtil/Random.cpp') diff --git a/cpp/src/IceUtil/Random.cpp b/cpp/src/IceUtil/Random.cpp index c9772de3b9a..7afc171c1a2 100644 --- a/cpp/src/IceUtil/Random.cpp +++ b/cpp/src/IceUtil/Random.cpp @@ -7,6 +7,10 @@ // // ********************************************************************** +#if defined(_MSC_VER) && (_MSC_VER > 1400) +# define _CRT_RAND_S +#endif + #include #include #include @@ -21,6 +25,7 @@ using namespace std; using namespace IceUtil; +#if !defined(_WIN32) || !defined(_MSC_VER) || (_MSC_VER < 1400) namespace { @@ -76,11 +81,19 @@ public: Init init; } +#endif void IceUtilInternal::generateRandom(char* buffer, int size) { #ifdef _WIN32 + +# if defined(_MSC_VER) && (_MSC_VER >= 1400) + for(int i = 0; i < size; ++i) + { + buffer[i] = random(256); + } +# else // // It's not clear from the Microsoft documentation if CryptGenRandom // can be called concurrently from several threads. To be on the safe @@ -101,6 +114,8 @@ IceUtilInternal::generateRandom(char* buffer, int size) { throw SyscallException(__FILE__, __LINE__, GetLastError()); } +# endif + #else // @@ -149,18 +164,25 @@ IceUtilInternal::generateRandom(char* buffer, int size) #endif } -int +unsigned int IceUtilInternal::random(int limit) { - int r; - generateRandom(reinterpret_cast(&r), static_cast(sizeof(int))); - if(limit > 0) + unsigned int r; +#if defined(_MSC_VER) && (_MSC_VER > 1400) + errno_t err; + if(err = rand_s(&r) != 0) { - r = r % limit; + SyscallException ex(__FILE__, __LINE__, errno); + cerr << "rand_s failed:\n" << ex << endl; + assert(0); + throw ex; } - if(r < 0) +#else + generateRandom(reinterpret_cast(&r), static_cast(sizeof(unsigned int))); +#endif + if(limit > 0) { - r = -r; + r = r % limit; } return r; } -- cgit v1.2.3