From e4e76f83c3e6c4de8ac467667df7daf05d2d9582 Mon Sep 17 00:00:00 2001
From: Monster Iestyn <iestynjealous@ntlworld.com>
Date: Sat, 27 Oct 2018 16:09:14 +0100
Subject: [PATCH] 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 6a21278662..c054b3a4a1 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;
 }
-- 
GitLab