|
Boost : |
From: David Deakins (ddeakins_at_[hidden])
Date: 2007-11-02 14:31:21
I ran across a case this morning where the FormatMessage Win32 call in
boost::system_error_category::message() fails (the error message table
did not have an entry for the error code that was passed).
Unfortunately, when this happens lpMsgBuf is not initialized to anything
and trying to use it can result in an access violation. The attached
patch adds checks to catch situations where FormatMessage fails.
Thanks,
-Dave
Index: error_code.cpp
===================================================================
--- error_code.cpp (revision 40686)
+++ error_code.cpp (working copy)
@@ -347,7 +347,7 @@
{
# ifndef BOOST_NO_ANSI_APIS
LPVOID lpMsgBuf;
- ::FormatMessageA(
+ DWORD retval = ::FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
@@ -358,11 +358,14 @@
0,
NULL
);
+ if (retval == 0)
+ return std::string();
+
std::string str( static_cast<LPCSTR>(lpMsgBuf) );
::LocalFree( lpMsgBuf ); // free the buffer
# else // WinCE workaround
LPVOID lpMsgBuf;
- ::FormatMessageW(
+ DWORD retval = ::FormatMessageW(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
@@ -373,10 +376,13 @@
0,
NULL
);
+ if (retval == 0)
+ return std::string();
int num_chars = (wcslen( static_cast<LPCWSTR>(lpMsgBuf) ) + 1) * 2;
LPSTR narrow_buffer = (LPSTR)_alloca( num_chars );
- ::WideCharToMultiByte(CP_ACP, 0, static_cast<LPCWSTR>(lpMsgBuf), -1, narrow_buffer, num_chars, NULL, NULL);
+ if (::WideCharToMultiByte(CP_ACP, 0, static_cast<LPCWSTR>(lpMsgBuf), -1, narrow_buffer, num_chars, NULL, NULL) == 0)
+ return std::string();
std::string str( narrow_buffer );
::LocalFree( lpMsgBuf ); // free the buffer
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk