Opened 13 years ago

Closed 12 years ago

#3474 closed Bugs (fixed)

Memory leak due to exception safety issue

Reported by: lee@… Owned by: Beman Dawes
Milestone: To Be Determined Component: system
Version: Boost 1.35.0 Severity: Problem
Keywords: Cc:

Description

The following code fragment form file 'libs/filesystem/src/exception.cpp' may leak memory if an exception is thrown while assigning 'lpMsgBuf' to 'target':

void system_message(system_error_type sys_err_code, std::string& target)
{
  LPVOID lpMsgBuf;
  ::FormatMessageA( 
      FORMAT_MESSAGE_ALLOCATE_BUFFER | 
      FORMAT_MESSAGE_FROM_SYSTEM | 
      FORMAT_MESSAGE_IGNORE_INSERTS,
      NULL,
      sys_err_code,
      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
      (LPSTR) &lpMsgBuf,
      0,
      NULL 
  );
  target += static_cast<LPCSTR>(lpMsgBuf);
  ::LocalFree( lpMsgBuf ); // free the buffer

  //...

Change History (3)

comment:1 by lee@…, 13 years ago

Milestone: Boost 1.41.0To Be Determined
Version: Boost 1.40.0Boost 1.35.0

comment:2 by Marshall Clow, 12 years ago

Component: filesystemsystem

As of boost 1.42.0, there are no calls to "FormatMessageA" in boost filesystem. There is similar code in the Boost.System library, though. Reassigning; Beman will squawk if I'm wrong. File: libs/system/src/error_code.cpp

  std::string system_error_category::message( int ev ) const
  {
# ifndef BOOST_NO_ANSI_APIS  
    LPVOID lpMsgBuf;
    DWORD retval = ::FormatMessageA( 
        FORMAT_MESSAGE_ALLOCATE_BUFFER | 
        FORMAT_MESSAGE_FROM_SYSTEM | 
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        ev,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
        (LPSTR) &lpMsgBuf,
        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

comment:3 by Beman Dawes, 12 years ago

Resolution: fixed
Status: newclosed

Fixed. See changeset 63184

Thanks,

--Beman

Note: See TracTickets for help on using tickets.