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: bemandawes
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 changed from Boost 1.41.0 to To Be Determined
  • Version changed from Boost 1.40.0 to Boost 1.35.0

comment:2 Changed 7 years ago by marshall

  • Component changed from filesystem to system

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 bemandawes

  • Resolution set to fixed
  • Status changed from new to closed

Fixed. See changeset 63184

Thanks,

--Beman

Add Comment

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain bemandawes.
The resolution will be deleted. Next status will be 'reopened'.
Author


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

 
Note: See TracTickets for help on using tickets.