diff options
author | Dwayne Boone <dwayne@zeroc.com> | 2009-09-08 15:56:07 -0230 |
---|---|---|
committer | Dwayne Boone <dwayne@zeroc.com> | 2009-09-08 15:56:07 -0230 |
commit | 3f6247914f665a141bec4a5091b1935a22ac94ef (patch) | |
tree | f61a2f346d3af469356ed505cb5556e1d75514df /cpp | |
parent | Bug 4246 - tolower/toupper on modify ascii (diff) | |
download | ice-3f6247914f665a141bec4a5091b1935a22ac94ef.tar.bz2 ice-3f6247914f665a141bec4a5091b1935a22ac94ef.tar.xz ice-3f6247914f665a141bec4a5091b1935a22ac94ef.zip |
Bug 3459 - use rand_s on windows for random number generation
Diffstat (limited to 'cpp')
-rw-r--r-- | cpp/include/IceUtil/Random.h | 2 | ||||
-rw-r--r-- | cpp/src/IceGrid/ObjectCache.cpp | 2 | ||||
-rw-r--r-- | cpp/src/IceGrid/ObjectCache.h | 2 | ||||
-rw-r--r-- | cpp/src/IceUtil/Random.cpp | 36 |
4 files changed, 32 insertions, 10 deletions
diff --git a/cpp/include/IceUtil/Random.h b/cpp/include/IceUtil/Random.h index f2057ddcf75..7ee965c50ce 100644 --- a/cpp/include/IceUtil/Random.h +++ b/cpp/include/IceUtil/Random.h @@ -17,7 +17,7 @@ namespace IceUtilInternal { ICE_UTIL_API void generateRandom(char*, int); -ICE_UTIL_API int random(int = 0); +ICE_UTIL_API unsigned int random(int = 0); } diff --git a/cpp/src/IceGrid/ObjectCache.cpp b/cpp/src/IceGrid/ObjectCache.cpp index 790c52af2ad..83a2a086695 100644 --- a/cpp/src/IceGrid/ObjectCache.cpp +++ b/cpp/src/IceGrid/ObjectCache.cpp @@ -19,7 +19,7 @@ using namespace std; using namespace IceGrid; -pointer_to_unary_function<int, int> ObjectCache::_rand(IceUtilInternal::random); +pointer_to_unary_function<int, unsigned int> ObjectCache::_rand(IceUtilInternal::random); namespace IceGrid { diff --git a/cpp/src/IceGrid/ObjectCache.h b/cpp/src/IceGrid/ObjectCache.h index ce6b2ab279a..3e068dcc0ba 100644 --- a/cpp/src/IceGrid/ObjectCache.h +++ b/cpp/src/IceGrid/ObjectCache.h @@ -77,7 +77,7 @@ private: const Ice::CommunicatorPtr _communicator; std::map<std::string, TypeEntry> _types; - static std::pointer_to_unary_function<int, int> _rand; + static std::pointer_to_unary_function<int, unsigned int> _rand; }; }; 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 <IceUtil/Random.h> #include <IceUtil/Mutex.h> #include <IceUtil/MutexPtrLock.h> @@ -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<char*>(&r), static_cast<int>(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<char*>(&r), static_cast<unsigned int>(sizeof(unsigned int))); +#endif + if(limit > 0) { - r = -r; + r = r % limit; } return r; } |