From a4f2303361396ed39881a705e5b55796d5a0681f Mon Sep 17 00:00:00 2001 From: Jan Knepper Date: Mon, 3 Jun 2019 22:31:37 +0200 Subject: [PATCH] Use closesocket() for closing sockets under MSW If accepting a socket connection failed, wxSocketImpl::Accept() used close() to close the socket even under MSW, but it can be only used for the file descriptors there and closesocket() must be used instead for the sockets. Add new (private) wxCloseSocket define and use it both here, to fix the bug, and elsewhere to make the code more clear. Closes #18407. --- include/wx/msw/private/sockmsw.h | 2 ++ include/wx/unix/private/sockunix.h | 4 +++- src/common/socket.cpp | 2 +- src/msw/sockmsw.cpp | 2 +- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/include/wx/msw/private/sockmsw.h b/include/wx/msw/private/sockmsw.h index 91dbbede2a..b79aa8e094 100644 --- a/include/wx/msw/private/sockmsw.h +++ b/include/wx/msw/private/sockmsw.h @@ -34,6 +34,8 @@ #define wxIoctlSocketArg_t u_long #endif +#define wxCloseSocket closesocket + // ---------------------------------------------------------------------------- // MSW-specific socket implementation // ---------------------------------------------------------------------------- diff --git a/include/wx/unix/private/sockunix.h b/include/wx/unix/private/sockunix.h index 3c0101ee29..05d043d354 100644 --- a/include/wx/unix/private/sockunix.h +++ b/include/wx/unix/private/sockunix.h @@ -23,6 +23,8 @@ #include "wx/private/fdiomanager.h" +#define wxCloseSocket close + class wxSocketImplUnix : public wxSocketImpl, public wxFDIOHandler { @@ -64,7 +66,7 @@ private: { DisableEvents(); - close(m_fd); + wxCloseSocket(m_fd); } virtual void UnblockAndRegisterWithEventLoop() wxOVERRIDE diff --git a/src/common/socket.cpp b/src/common/socket.cpp index 207b3c9751..eb971f952f 100644 --- a/src/common/socket.cpp +++ b/src/common/socket.cpp @@ -529,7 +529,7 @@ wxSocketImpl *wxSocketImpl::Accept(wxSocketBase& wxsocket) WX_SOCKLEN_T fromlen = sizeof(from); const wxSOCKET_T fd = accept(m_fd, &from.addr, &fromlen); - wxScopeGuard closeSocket = wxMakeGuard(wxClose, fd); + wxScopeGuard closeSocket = wxMakeGuard(wxCloseSocket, fd); // accepting is similar to reading in the sense that it resets "ready for // read" flag on the socket diff --git a/src/msw/sockmsw.cpp b/src/msw/sockmsw.cpp index 2a66c8eda4..4dbd6a6b4e 100644 --- a/src/msw/sockmsw.cpp +++ b/src/msw/sockmsw.cpp @@ -310,7 +310,7 @@ void wxSocketImplMSW::DoClose() { wxSocketManager::Get()->Uninstall_Callback(this); - closesocket(m_fd); + wxCloseSocket(m_fd); } wxSocketError wxSocketImplMSW::GetLastError() const