From e0c466534b30fcf5907d341a826b59619a381ef7 Mon Sep 17 00:00:00 2001 From: Beman Dawes Date: Sat, 3 Nov 2007 00:49:07 +0000 Subject: [PATCH] Detect Windows FormatMessage errors, standardize message() return on message-not-found errors [SVN r40710] --- src/error_code.cpp | 20 +++++++++++++------- test/error_code_test.cpp | 13 +++++++++++++ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/error_code.cpp b/src/error_code.cpp index 7c029dc..02a4cf8 100644 --- a/src/error_code.cpp +++ b/src/error_code.cpp @@ -88,7 +88,7 @@ namespace || (defined(__osf__) && !defined(_REENTRANT))\ || (defined(__vms)) 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 char buf[64]; char * bp = buf; @@ -96,7 +96,7 @@ namespace # if defined(__CYGWIN__) || defined(__USE_GNU) // Oddball version of strerror_r 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 // POSIX version of strerror_r int result; @@ -107,7 +107,7 @@ namespace # if defined (__sgi) const char * c_str = strerror( ev ); result = 0; - return std::string( c_str ? c_str : "invalid_argument" ); + return std::string( c_str ? c_str : "Unknown error" ); # else result = strerror_r( ev, bp, sz ); # endif @@ -128,7 +128,7 @@ namespace } 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 ); sz = 0; return msg; @@ -347,7 +347,7 @@ namespace { # 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 @@ namespace 0, NULL ); + if (retval == 0) + return std::string("Unknown error"); + std::string str( static_cast(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 @@ namespace 0, NULL ); + if (retval == 0) + return std::string("Unknown error"); int num_chars = (wcslen( static_cast(lpMsgBuf) ) + 1) * 2; LPSTR narrow_buffer = (LPSTR)_alloca( num_chars ); - ::WideCharToMultiByte(CP_ACP, 0, static_cast(lpMsgBuf), -1, narrow_buffer, num_chars, NULL, NULL); + if (::WideCharToMultiByte(CP_ACP, 0, static_cast(lpMsgBuf), -1, narrow_buffer, num_chars, NULL, NULL) == 0) + return std::string("Unknown error"); std::string str( narrow_buffer ); ::LocalFree( lpMsgBuf ); // free the buffer diff --git a/test/error_code_test.cpp b/test/error_code_test.cpp index 12eb1f2..a6de9a2 100644 --- a/test/error_code_test.cpp +++ b/test/error_code_test.cpp @@ -144,6 +144,19 @@ int test_main( int, char ** ) BOOST_CHECK( system_category == native_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 std::cout << "Windows tests...\n"; // these tests probe the Windows posix decoder