forked from boostorg/system
Detect Windows FormatMessage errors, standardize message() return on message-not-found errors
[SVN r40710]
This commit is contained in:
@@ -88,7 +88,7 @@ namespace
|
|||||||
|| (defined(__osf__) && !defined(_REENTRANT))\
|
|| (defined(__osf__) && !defined(_REENTRANT))\
|
||||||
|| (defined(__vms))
|
|| (defined(__vms))
|
||||||
const char * c_str = std::strerror( ev );
|
const char * c_str = std::strerror( ev );
|
||||||
return std::string( c_str ? c_str : "invalid_argument" );
|
return std::string( c_str ? c_str : "Unknown error" );
|
||||||
# else
|
# else
|
||||||
char buf[64];
|
char buf[64];
|
||||||
char * bp = buf;
|
char * bp = buf;
|
||||||
@@ -96,7 +96,7 @@ namespace
|
|||||||
# if defined(__CYGWIN__) || defined(__USE_GNU)
|
# if defined(__CYGWIN__) || defined(__USE_GNU)
|
||||||
// Oddball version of strerror_r
|
// Oddball version of strerror_r
|
||||||
const char * c_str = strerror_r( ev, bp, sz );
|
const char * c_str = strerror_r( ev, bp, sz );
|
||||||
return std::string( c_str ? c_str : "invalid_argument" );
|
return std::string( c_str ? c_str : "Unknown error" );
|
||||||
# else
|
# else
|
||||||
// POSIX version of strerror_r
|
// POSIX version of strerror_r
|
||||||
int result;
|
int result;
|
||||||
@@ -107,7 +107,7 @@ namespace
|
|||||||
# if defined (__sgi)
|
# if defined (__sgi)
|
||||||
const char * c_str = strerror( ev );
|
const char * c_str = strerror( ev );
|
||||||
result = 0;
|
result = 0;
|
||||||
return std::string( c_str ? c_str : "invalid_argument" );
|
return std::string( c_str ? c_str : "Unknown error" );
|
||||||
# else
|
# else
|
||||||
result = strerror_r( ev, bp, sz );
|
result = strerror_r( ev, bp, sz );
|
||||||
# endif
|
# endif
|
||||||
@@ -128,7 +128,7 @@ namespace
|
|||||||
}
|
}
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
std::string msg( ( result == invalid_argument ) ? "invalid_argument" : bp );
|
std::string msg( ( result == invalid_argument ) ? "Unknown error" : bp );
|
||||||
if ( sz > sizeof(buf) ) std::free( bp );
|
if ( sz > sizeof(buf) ) std::free( bp );
|
||||||
sz = 0;
|
sz = 0;
|
||||||
return msg;
|
return msg;
|
||||||
@@ -347,7 +347,7 @@ namespace
|
|||||||
{
|
{
|
||||||
# ifndef BOOST_NO_ANSI_APIS
|
# ifndef BOOST_NO_ANSI_APIS
|
||||||
LPVOID lpMsgBuf;
|
LPVOID lpMsgBuf;
|
||||||
::FormatMessageA(
|
DWORD retval = ::FormatMessageA(
|
||||||
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
||||||
FORMAT_MESSAGE_FROM_SYSTEM |
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
||||||
FORMAT_MESSAGE_IGNORE_INSERTS,
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||||
@@ -358,11 +358,14 @@ namespace
|
|||||||
0,
|
0,
|
||||||
NULL
|
NULL
|
||||||
);
|
);
|
||||||
|
if (retval == 0)
|
||||||
|
return std::string("Unknown error");
|
||||||
|
|
||||||
std::string str( static_cast<LPCSTR>(lpMsgBuf) );
|
std::string str( static_cast<LPCSTR>(lpMsgBuf) );
|
||||||
::LocalFree( lpMsgBuf ); // free the buffer
|
::LocalFree( lpMsgBuf ); // free the buffer
|
||||||
# else // WinCE workaround
|
# else // WinCE workaround
|
||||||
LPVOID lpMsgBuf;
|
LPVOID lpMsgBuf;
|
||||||
::FormatMessageW(
|
DWORD retval = ::FormatMessageW(
|
||||||
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
||||||
FORMAT_MESSAGE_FROM_SYSTEM |
|
FORMAT_MESSAGE_FROM_SYSTEM |
|
||||||
FORMAT_MESSAGE_IGNORE_INSERTS,
|
FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||||
@@ -373,10 +376,13 @@ namespace
|
|||||||
0,
|
0,
|
||||||
NULL
|
NULL
|
||||||
);
|
);
|
||||||
|
if (retval == 0)
|
||||||
|
return std::string("Unknown error");
|
||||||
|
|
||||||
int num_chars = (wcslen( static_cast<LPCWSTR>(lpMsgBuf) ) + 1) * 2;
|
int num_chars = (wcslen( static_cast<LPCWSTR>(lpMsgBuf) ) + 1) * 2;
|
||||||
LPSTR narrow_buffer = (LPSTR)_alloca( num_chars );
|
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("Unknown error");
|
||||||
|
|
||||||
std::string str( narrow_buffer );
|
std::string str( narrow_buffer );
|
||||||
::LocalFree( lpMsgBuf ); // free the buffer
|
::LocalFree( lpMsgBuf ); // free the buffer
|
||||||
|
@@ -144,6 +144,19 @@ int test_main( int, char ** )
|
|||||||
BOOST_CHECK( system_category == native_ecat );
|
BOOST_CHECK( system_category == native_ecat );
|
||||||
BOOST_CHECK( posix_category == errno_ecat );
|
BOOST_CHECK( posix_category == errno_ecat );
|
||||||
|
|
||||||
|
// test error_code and error_condition message(),
|
||||||
|
// see Boost.Filesystem operations_test for code specific message() tests
|
||||||
|
ec = error_code( -1, system_category );
|
||||||
|
BOOST_CHECK( ec.message() == "Unknown error" );
|
||||||
|
ec = error_code( BOOST_ACCESS_ERROR_MACRO, system_category );
|
||||||
|
BOOST_CHECK( ec.message() != "" );
|
||||||
|
BOOST_CHECK( ec.message() != "Unknown error" );
|
||||||
|
dec = error_condition( -1, posix_category );
|
||||||
|
BOOST_CHECK( dec.message() == "Unknown error" );
|
||||||
|
dec = error_condition( BOOST_ACCESS_ERROR_MACRO, posix_category );
|
||||||
|
BOOST_CHECK( dec.message() != "" );
|
||||||
|
BOOST_CHECK( dec.message() != "Unknown error" );
|
||||||
|
|
||||||
#ifdef BOOST_WINDOWS_API
|
#ifdef BOOST_WINDOWS_API
|
||||||
std::cout << "Windows tests...\n";
|
std::cout << "Windows tests...\n";
|
||||||
// these tests probe the Windows posix decoder
|
// these tests probe the Windows posix decoder
|
||||||
|
Reference in New Issue
Block a user