summaryrefslogtreecommitdiff
path: root/cpp/src/IceUtil/StringUtil.cpp
diff options
context:
space:
mode:
authorBernard Normier <bernard@zeroc.com>2012-10-08 13:43:06 -0400
committerBernard Normier <bernard@zeroc.com>2012-10-08 13:43:06 -0400
commit29a33cd4db7eb3b31b209429626a5749adc00658 (patch)
tree2c41e16711a7bb9be384a620eb1ac86587df7fc2 /cpp/src/IceUtil/StringUtil.cpp
parentFixed IceStorm build failure on Windows 64 bits (diff)
downloadice-29a33cd4db7eb3b31b209429626a5749adc00658.tar.bz2
ice-29a33cd4db7eb3b31b209429626a5749adc00658.tar.xz
ice-29a33cd4db7eb3b31b209429626a5749adc00658.zip
removed first "dummy" parameter for getIn in Ice/custom/AllTests.cpp
Fixed ICE-4877: - StringUtil.cpp: replaced auto_ptr by ScopedArray and added 'size' parameter to FormatMessageW on WinRT - ServiceInstaller.cpp: replaced auto_ptr by ScopedArray and fixed SID memory
Diffstat (limited to 'cpp/src/IceUtil/StringUtil.cpp')
-rw-r--r--cpp/src/IceUtil/StringUtil.cpp124
1 files changed, 70 insertions, 54 deletions
diff --git a/cpp/src/IceUtil/StringUtil.cpp b/cpp/src/IceUtil/StringUtil.cpp
index a5fb7a35373..ec54ef9cf9c 100644
--- a/cpp/src/IceUtil/StringUtil.cpp
+++ b/cpp/src/IceUtil/StringUtil.cpp
@@ -11,6 +11,10 @@
#include <IceUtil/Unicode.h>
#include <cstring>
+#ifdef ICE_OS_WINRT
+#include <IceUtil/ScopedArray.h>
+#endif
+
using namespace std;
using namespace IceUtil;
@@ -496,67 +500,79 @@ IceUtilInternal::errorToString(int error, LPCVOID source)
{
if(error < WSABASEERR)
{
-#ifndef ICE_OS_WINRT
- LPVOID lpMsgBuf = 0;
-#else
- int size = 256;
- auto_ptr<BYTE> lpMsgBuf = auto_ptr<BYTE>(new BYTE[size]);
-#endif
- DWORD ok = 0;
-#ifdef ICE_OS_WINRT
- while(true)
- {
-#endif
- ok = FormatMessageW(
-#ifndef ICE_OS_WINRT
- FORMAT_MESSAGE_ALLOCATE_BUFFER |
-#endif
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS |
- (source != NULL ? FORMAT_MESSAGE_FROM_HMODULE : 0),
- source,
- error,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
-#ifndef ICE_OS_WINRT
- (LPWSTR)&lpMsgBuf,
-#else
- (LPWSTR)lpMsgBuf.get(),
-#endif
- 0,
- NULL);
#ifdef ICE_OS_WINRT
- if(!ok && size < 65536)
- {
- DWORD err = GetLastError();
- if(err == ERROR_INSUFFICIENT_BUFFER)
- {
- size *= 4;
- size = max(size, 65536);
- lpMsgBuf = auto_ptr<BYTE>(new BYTE[size]);
- continue;
- }
- }
- break;
- }
-#endif
- if(ok)
+ int size = 256;
+ IceUtil::ScopedArray<wchar_t> lpMsgBuf(new wchar_t[size]);
+
+ DWORD stored = 0;
+
+ while(stored == 0)
{
-#ifndef ICE_OS_WINRT
- LPWSTR msg = (LPWSTR)lpMsgBuf;
+ stored = FormatMessageW(
+ FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_IGNORE_INSERTS |
+ (source != NULL ? FORMAT_MESSAGE_FROM_HMODULE : 0),
+ source,
+ error,
+ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
+ lpMsgBuf.get(),
+ size,
+ NULL);
+
+ if(stored == 0)
+ {
+ DWORD err = GetLastError();
+ if(err == ERROR_INSUFFICIENT_BUFFER)
+ {
+ if(size == 65536)
+ {
+ break; // already at the max size
+ }
+ else
+ {
+ size *= 4;
+ size = max(size, 65536);
+ lpMsgBuf.reset(new wchar_t[size]);
+ }
+ }
+ else
+ {
+ break;
+ }
+ }
+ }
+
+ LPWSTR msg = lpMsgBuf.get();
+
#else
- LPWSTR msg = (LPWSTR)lpMsgBuf.get();
+ LPWSTR msg = 0;
+
+ DWORD stored = FormatMessageW(
+ FORMAT_MESSAGE_ALLOCATE_BUFFER |
+ FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_IGNORE_INSERTS |
+ (source != NULL ? FORMAT_MESSAGE_FROM_HMODULE : 0),
+ source,
+ error,
+ MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
+ reinterpret_cast<LPWSTR>(&msg),
+ 0,
+ NULL);
#endif
- assert(msg && wcslen((const wchar_t*)msg) > 0);
- wstring result = (const wchar_t*)msg;
- if(result[result.length() - 1] == '\n')
+
+ if(stored > 0)
+ {
+ assert(msg && wcslen(msg) > 0);
+ wstring result = msg;
+ if(result[result.length() - 1] == L'\n')
{
result = result.substr(0, result.length() - 2);
}
#ifndef ICE_OS_WINRT
- if(lpMsgBuf)
+ if(msg)
{
- LocalFree(lpMsgBuf);
+ LocalFree(msg);
}
#endif
return IceUtil::wstringToString(result);
@@ -564,9 +580,9 @@ IceUtilInternal::errorToString(int error, LPCVOID source)
else
{
#ifndef ICE_OS_WINRT
- if(lpMsgBuf)
+ if(msg)
{
- LocalFree(lpMsgBuf);
+ LocalFree(msg);
}
#endif
ostringstream os;
@@ -574,7 +590,7 @@ IceUtilInternal::errorToString(int error, LPCVOID source)
return os.str();
}
}
-
+
switch(error)
{
case WSAEINTR: