Boost logo

Boost-Commit :

From: bdawes_at_[hidden]
Date: 2007-11-02 20:49:08


Author: bemandawes
Date: 2007-11-02 20:49:07 EDT (Fri, 02 Nov 2007)
New Revision: 40710
URL: http://svn.boost.org/trac/boost/changeset/40710

Log:
Detect Windows FormatMessage errors, standardize message() return on message-not-found errors
Text files modified:
   trunk/libs/system/src/error_code.cpp | 20 +++++++++++++-------
   trunk/libs/system/test/error_code_test.cpp | 13 +++++++++++++
   2 files changed, 26 insertions(+), 7 deletions(-)

Modified: trunk/libs/system/src/error_code.cpp
==============================================================================
--- trunk/libs/system/src/error_code.cpp (original)
+++ trunk/libs/system/src/error_code.cpp 2007-11-02 20:49:07 EDT (Fri, 02 Nov 2007)
@@ -88,7 +88,7 @@
      || (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 @@
   # 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 @@
   # 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 @@
       }
       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 @@
   {
 # 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("Unknown error");
+
     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("Unknown error");
     
     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("Unknown error");
 
     std::string str( narrow_buffer );
     ::LocalFree( lpMsgBuf ); // free the buffer

Modified: trunk/libs/system/test/error_code_test.cpp
==============================================================================
--- trunk/libs/system/test/error_code_test.cpp (original)
+++ trunk/libs/system/test/error_code_test.cpp 2007-11-02 20:49:07 EDT (Fri, 02 Nov 2007)
@@ -144,6 +144,19 @@
   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


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk