Modify

Opened 8 years ago

Closed 7 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

  //...

Attachments (0)

Change History (3)

comment:1 Changed 8 years ago by lee@…

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

comment:2 Changed 7 years ago by Marshall Clow

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 Changed 7 years ago by Beman Dawes

Resolution: fixed
Status: newclosed

Fixed. See changeset 63184

Thanks,

--Beman

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain Beman Dawes.
The resolution will be deleted.

Add Comment


E-mail address and name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.