Modify

Ticket #3474 (closed Bugs: fixed)

Opened 5 years ago

Last modified 4 years ago

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

Change History

comment:1 Changed 5 years ago by lee@…

  • Version changed from Boost 1.40.0 to Boost 1.35.0
  • Milestone changed from Boost 1.41.0 to To Be Determined

comment:2 Changed 4 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 4 years ago by bemandawes

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

Fixed. See changeset 63184

Thanks,

--Beman

View

Add a comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
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.