From 3d4661635e093f1fddd00cb591880d8f81bace74 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Tue, 5 Mar 2019 23:30:24 -0800 Subject: [PATCH] Fix connect() socket error trace on Windows closesocket() clobbers the Winsock last error value, so we must only call it after tracing. --- miniupnpc/connecthostport.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/miniupnpc/connecthostport.c b/miniupnpc/connecthostport.c index be5e4c3..63e5ee0 100644 --- a/miniupnpc/connecthostport.c +++ b/miniupnpc/connecthostport.c @@ -193,6 +193,8 @@ SOCKET connecthostport(const char * host, unsigned short port, s = INVALID_SOCKET; for(p = ai; p; p = p->ai_next) { + if(!ISINVALID(s)) + closesocket(s); s = socket(p->ai_family, p->ai_socktype, p->ai_protocol); if(ISINVALID(s)) continue; @@ -260,7 +262,6 @@ SOCKET connecthostport(const char * host, unsigned short port, #endif /* #ifdef MINIUPNPC_IGNORE_EINTR */ if(n < 0) { - closesocket(s); continue; } else @@ -277,6 +278,7 @@ SOCKET connecthostport(const char * host, unsigned short port, if(n < 0) { PRINT_SOCKET_ERROR("connect"); + closesocket(s); return INVALID_SOCKET; } #endif /* #ifdef USE_GETHOSTBYNAME */