summaryrefslogtreecommitdiff
path: root/cpp
diff options
context:
space:
mode:
authorBernard Normier <bernard@zeroc.com>2014-07-28 16:53:43 -0400
committerBernard Normier <bernard@zeroc.com>2014-07-28 16:53:43 -0400
commitbf89a1b51b56231fddb9939596a39b5c7d86a1e0 (patch)
tree43698ed84b34b7bb4f54d33b398b063220715872 /cpp
parentCorrect __declspec for MinGW (diff)
downloadice-bf89a1b51b56231fddb9939596a39b5c7d86a1e0.tar.bz2
ice-bf89a1b51b56231fddb9939596a39b5c7d86a1e0.tar.xz
ice-bf89a1b51b56231fddb9939596a39b5c7d86a1e0.zip
Fixed ICE-5598 (generateRandom) and ICE-5520 (removed openssl/patch.mingw)
Diffstat (limited to 'cpp')
-rw-r--r--cpp/include/IceUtil/Random.h2
-rw-r--r--cpp/src/Glacier2/RouterI.cpp2
-rw-r--r--cpp/src/IceUtil/Random.cpp48
-rw-r--r--cpp/src/IceUtil/UUID.cpp2
-rw-r--r--cpp/src/IceWS/TransceiverI.cpp2
-rw-r--r--cpp/test/IceUtil/uuid/Client.cpp9
6 files changed, 40 insertions, 25 deletions
diff --git a/cpp/include/IceUtil/Random.h b/cpp/include/IceUtil/Random.h
index 731edd2fd13..954a6d6a2d5 100644
--- a/cpp/include/IceUtil/Random.h
+++ b/cpp/include/IceUtil/Random.h
@@ -16,7 +16,7 @@
namespace IceUtilInternal
{
-ICE_UTIL_API void generateRandom(char*, int);
+ICE_UTIL_API void generateRandom(char*, size_t);
ICE_UTIL_API unsigned int random(int = 0);
}
diff --git a/cpp/src/Glacier2/RouterI.cpp b/cpp/src/Glacier2/RouterI.cpp
index 4589add1d53..9e94758b8aa 100644
--- a/cpp/src/Glacier2/RouterI.cpp
+++ b/cpp/src/Glacier2/RouterI.cpp
@@ -50,7 +50,7 @@ Glacier2::RouterI::RouterI(const InstancePtr& instance, const ConnectionPtr& con
ident.name = "dummy";
ident.category.resize(20);
char buf[20];
- IceUtilInternal::generateRandom(buf, static_cast<int>(sizeof(buf)));
+ IceUtilInternal::generateRandom(buf, sizeof(buf));
for(unsigned int i = 0; i < sizeof(buf); ++i)
{
const unsigned char c = static_cast<unsigned char>(buf[i]); // A value between 0-255
diff --git a/cpp/src/IceUtil/Random.cpp b/cpp/src/IceUtil/Random.cpp
index 1df50d27855..f5f6773def3 100644
--- a/cpp/src/IceUtil/Random.cpp
+++ b/cpp/src/IceUtil/Random.cpp
@@ -93,12 +93,34 @@ Init init;
#endif
void
-IceUtilInternal::generateRandom(char* buffer, int size)
+IceUtilInternal::generateRandom(char* buffer, size_t size)
{
#ifdef _WIN32
- for(int i = 0; i < size; ++i)
+ int i = 0;
+ const size_t randSize = sizeof(unsigned int);
+
+ while(size - i >= randSize)
{
- buffer[i] = random(256);
+ unsigned int r = 0;
+ errno_t err = rand_s(&r);
+ if(err != 0)
+ {
+ throw SyscallException(__FILE__, __LINE__, errno);
+ }
+ memcpy(buffer + i, &r, randSize);
+ i += randSize;
+ }
+
+ if(size - i > 0)
+ {
+ assert(size - i < randSize);
+ unsigned int r = 0;
+ errno_t err = rand_s(&r);
+ if(err != 0)
+ {
+ throw SyscallException(__FILE__, __LINE__, errno);
+ }
+ memcpy(buffer + i, &r, size - i);
}
#else
//
@@ -110,7 +132,6 @@ IceUtilInternal::generateRandom(char* buffer, int size)
fd = open("/dev/urandom", O_RDONLY);
if(fd == -1)
{
- assert(0);
throw SyscallException(__FILE__, __LINE__, errno);
}
}
@@ -121,16 +142,13 @@ IceUtilInternal::generateRandom(char* buffer, int size)
//
int reads = 0;
size_t index = 0;
- while(reads <= 20 && index != static_cast<size_t>(size))
+ while(reads <= 20 && index != size)
{
- ssize_t bytesRead = read(fd, buffer + index, static_cast<size_t>(size) - index);
+ ssize_t bytesRead = read(fd, buffer + index, size - index);
if(bytesRead == -1 && errno != EINTR)
{
- SyscallException ex(__FILE__, __LINE__, errno);
- cerr << "Reading /dev/urandom failed:\n" << ex << endl;
- assert(0);
- throw ex;
+ throw SyscallException(__FILE__, __LINE__, errno);
}
else
{
@@ -139,9 +157,8 @@ IceUtilInternal::generateRandom(char* buffer, int size)
}
}
- if(index != static_cast<size_t>(size))
+ if(index != size)
{
- assert(0);
throw SyscallException(__FILE__, __LINE__, 0);
}
#endif
@@ -155,13 +172,10 @@ IceUtilInternal::random(int limit)
errno_t err = rand_s(&r);
if(err != 0)
{
- SyscallException ex(__FILE__, __LINE__, errno);
- cerr << "rand_s failed:\n" << ex << endl;
- assert(0);
- throw ex;
+ throw SyscallException(__FILE__, __LINE__, errno);
}
#else
- generateRandom(reinterpret_cast<char*>(&r), static_cast<unsigned int>(sizeof(unsigned int)));
+ generateRandom(reinterpret_cast<char*>(&r), sizeof(unsigned int));
#endif
if(limit > 0)
{
diff --git a/cpp/src/IceUtil/UUID.cpp b/cpp/src/IceUtil/UUID.cpp
index c64c8cb1b70..3e7a25dff8b 100644
--- a/cpp/src/IceUtil/UUID.cpp
+++ b/cpp/src/IceUtil/UUID.cpp
@@ -135,7 +135,7 @@ IceUtil::generateUUID()
// Randoms by the last 15 bits of the process id.
//
char* buffer = reinterpret_cast<char*>(&uuid);
- IceUtilInternal::generateRandom(buffer, static_cast<int>(sizeof(UUID)));
+ IceUtilInternal::generateRandom(buffer, sizeof(UUID));
//
// Adjust the bits that say "version 4" UUID
diff --git a/cpp/src/IceWS/TransceiverI.cpp b/cpp/src/IceWS/TransceiverI.cpp
index 11bc6fdd750..3c4f8ad5127 100644
--- a/cpp/src/IceWS/TransceiverI.cpp
+++ b/cpp/src/IceWS/TransceiverI.cpp
@@ -238,7 +238,7 @@ IceWS::TransceiverI::initialize(Buffer& readBuffer, Buffer& writeBuffer, bool& h
// encoded with Base64.
//
vector<unsigned char> key(16);
- IceUtilInternal::generateRandom(reinterpret_cast<char*>(&key[0]), static_cast<int>(key.size()));
+ IceUtilInternal::generateRandom(reinterpret_cast<char*>(&key[0]), key.size());
_key = IceInternal::Base64::encode(key);
out << _key << "\r\n\r\n"; // EOM
diff --git a/cpp/test/IceUtil/uuid/Client.cpp b/cpp/test/IceUtil/uuid/Client.cpp
index eed9061fce7..07b7a806868 100644
--- a/cpp/test/IceUtil/uuid/Client.cpp
+++ b/cpp/test/IceUtil/uuid/Client.cpp
@@ -109,13 +109,14 @@ struct GenerateRandomString
operator()()
{
string s;
- s.resize(20);
- char buf[20];
- IceUtilInternal::generateRandom(buf, static_cast<int>(sizeof(buf)));
+ s.resize(21);
+ char buf[21];
+ IceUtilInternal::generateRandom(buf, sizeof(buf));
for(unsigned int i = 0; i < sizeof(buf); ++i)
{
- s[i] = 33 + buf[i] % (127-33); // We use ASCII 33-126 (from ! to ~, w/o space).
+ s[i] = 33 + static_cast<unsigned char>(buf[i]) % (127 - 33); // We use ASCII 33-126 (from ! to ~, w/o space).
}
+ // cerr << s << endl;
return s;
}
};