From ab38e6cebb7765e1fe0ce846e2a30deab285e7d2 Mon Sep 17 00:00:00 2001 From: Monster Iestyn Date: Sat, 13 Oct 2018 20:44:01 +0100 Subject: [PATCH 1/5] Creating a quick get_WSAErrorStr function to act as a wrapper for FormatMessageA so we can string-ify Winsock errors properly Untested! --- src/i_tcp.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/i_tcp.c b/src/i_tcp.c index 6488e9845..9febd56f0 100644 --- a/src/i_tcp.c +++ b/src/i_tcp.c @@ -262,6 +262,28 @@ static void wattcp_outch(char s) } #endif +#ifdef USE_WINSOCK +// stupid microsoft makes things complicated +static inline char *get_WSAErrorStr(int e) +{ + char *buf = NULL; + + FormatMessageA( + FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, + (DWORD)e, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPTSTR)&buf, + 0, NULL); + + return buf; +} +#undef strerror +#define strerror get_WSAErrorStr +#endif + #ifdef USE_WINSOCK2 #define inet_ntop inet_ntopA #define HAVE_NTOP From 3b39a25adec80437e56f8a6d886d220786a889f6 Mon Sep 17 00:00:00 2001 From: Monster Iestyn Date: Sat, 27 Oct 2018 15:49:04 +0100 Subject: [PATCH 2/5] Save the result of errno (aka WSAGetLastError() for WinSock) as soon as possible, to prevent anything in SOCK_GetNodeAddress resetting the value to 0 while trying to print the message for the error itself! --- src/i_tcp.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/i_tcp.c b/src/i_tcp.c index 9febd56f0..6a2127866 100644 --- a/src/i_tcp.c +++ b/src/i_tcp.c @@ -781,9 +781,13 @@ static void SOCK_Send(void) &clientaddress[doomcom->remotenode].any, d); } - if (c == ERRSOCKET && errno != ECONNREFUSED && errno != EWOULDBLOCK) - I_Error("SOCK_Send, error sending to node %d (%s) #%u: %s", doomcom->remotenode, - SOCK_GetNodeAddress(doomcom->remotenode), errno, strerror(errno)); + if (c == ERRSOCKET) + { + int e = errno; // save error code so it can't be modified later + if (e != ECONNREFUSED && e != EWOULDBLOCK) + I_Error("SOCK_Send, error sending to node %d (%s) #%u: %s", doomcom->remotenode, + SOCK_GetNodeAddress(doomcom->remotenode), e, strerror(e)); + } } #endif From e4e76f83c3e6c4de8ac467667df7daf05d2d9582 Mon Sep 17 00:00:00 2001 From: Monster Iestyn Date: Sat, 27 Oct 2018 16:09:14 +0100 Subject: [PATCH 3/5] Use temporary buffer with a max size of 255 bytes instead of having Microsoft's FormatMessageA alloc one for us. Also, provide a fallback message in case no message was available for some reason --- src/i_tcp.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/i_tcp.c b/src/i_tcp.c index 6a2127866..c054b3a4a 100644 --- a/src/i_tcp.c +++ b/src/i_tcp.c @@ -266,17 +266,22 @@ static void wattcp_outch(char s) // stupid microsoft makes things complicated static inline char *get_WSAErrorStr(int e) { - char *buf = NULL; + char buf[256]; // allow up to 255 bytes + + buf[0] = '\0'; FormatMessageA( - FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, (DWORD)e, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPTSTR)&buf, - 0, NULL); + (LPTSTR)buf, + sizeof (buf), + NULL); + + if (!buf[0]) // provide a fallback error message if no message is available for some reason + sprintf(buf, "Unknown error"); return buf; } From bb3d850bbfaf9fe9c333e1c0c4a79706ec878c8c Mon Sep 17 00:00:00 2001 From: Monster Iestyn Date: Sat, 27 Oct 2018 16:27:00 +0100 Subject: [PATCH 4/5] static the buffer, forgot to do this earlier --- src/i_tcp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/i_tcp.c b/src/i_tcp.c index c054b3a4a..43dba4aa3 100644 --- a/src/i_tcp.c +++ b/src/i_tcp.c @@ -266,7 +266,7 @@ static void wattcp_outch(char s) // stupid microsoft makes things complicated static inline char *get_WSAErrorStr(int e) { - char buf[256]; // allow up to 255 bytes + static char buf[256]; // allow up to 255 bytes buf[0] = '\0'; From 9fb301ecb587ed933f300cc9b65f23f8072e9d57 Mon Sep 17 00:00:00 2001 From: Monster Iestyn Date: Sat, 27 Oct 2018 16:47:56 +0100 Subject: [PATCH 5/5] don't bother with inlining the function, on second thoughts --- src/i_tcp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/i_tcp.c b/src/i_tcp.c index 43dba4aa3..0728c7aac 100644 --- a/src/i_tcp.c +++ b/src/i_tcp.c @@ -264,7 +264,7 @@ static void wattcp_outch(char s) #ifdef USE_WINSOCK // stupid microsoft makes things complicated -static inline char *get_WSAErrorStr(int e) +static char *get_WSAErrorStr(int e) { static char buf[256]; // allow up to 255 bytes