From 76d296d67d6782975b32381c817487461f4972bc Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 21 Feb 2014 00:51:38 +0000 Subject: [PATCH] Fix socket leak in Accept() in case of error.o We leaked a socket descriptor if creating the accepted socket object failed. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@75952 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/common/socket.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/common/socket.cpp b/src/common/socket.cpp index bcb331fcc3..b39c9c6752 100644 --- a/src/common/socket.cpp +++ b/src/common/socket.cpp @@ -38,6 +38,7 @@ #include "wx/apptrait.h" #include "wx/sckaddr.h" +#include "wx/scopeguard.h" #include "wx/stopwatch.h" #include "wx/thread.h" #include "wx/evtloop.h" @@ -527,6 +528,8 @@ wxSocketImpl *wxSocketImpl::Accept(wxSocketBase& wxsocket) WX_SOCKLEN_T fromlen = sizeof(from); const wxSOCKET_T fd = accept(m_fd, &from.addr, &fromlen); + wxScopeGuard closeSocket = wxMakeGuard(close, fd); + // accepting is similar to reading in the sense that it resets "ready for // read" flag on the socket ReenableEvents(wxSOCKET_INPUT_FLAG); @@ -542,6 +545,8 @@ wxSocketImpl *wxSocketImpl::Accept(wxSocketBase& wxsocket) if ( !sock ) return NULL; + // Ownership of the socket now passes to wxSocketImpl object. + closeSocket.Dismiss(); sock->m_fd = fd; sock->m_peer = wxSockAddressImpl(from.addr, fromlen);