Fix gai_strerror() thread-safety on Windows.
authorThomas Munro <tmunro@postgresql.org>
Sun, 11 Feb 2024 21:47:57 +0000 (10:47 +1300)
committerThomas Munro <tmunro@postgresql.org>
Sun, 11 Feb 2024 22:14:21 +0000 (11:14 +1300)
Commit 5579388d removed code that supplied a fallback implementation of
getaddrinfo(), which was dead code on modern systems.  One tiny piece of
the removed code was still doing something useful on Windows, though:
that OS's own gai_strerror()/gai_strerrorA() function returns a pointer
to a static buffer that it overwrites each time, so it's not
thread-safe.  In rare circumstances, a multi-threaded client program
could get an incorrect or corrupted error message.

Restore the replacement gai_strerror() function, though now that it's
only for Windows we can put it into a win32-specific file and cut it
down to the errors that Windows documents.  The error messages here are
taken from FreeBSD, because Windows' own messages seemed too verbose.

Back-patch to 16.

Reviewed-by: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Discussion: https://postgr.es/m/CA%2BhUKGKz%2BF9d2PTiXwfYV7qJw%2BWg2jzACgSDgPizUw7UG%3Di58A%40mail.gmail.com

configure
configure.ac
src/include/port/win32/sys/socket.h
src/port/meson.build
src/port/win32gai_strerror.c [new file with mode: 0644]

index 2a1ee251f2d7206790f56e536ed2faee0b192e39..6b87e5c9a8c3f1252199227ae21f2368dffd30b0 100755 (executable)
--- a/configure
+++ b/configure
@@ -16388,6 +16388,12 @@ esac
  ;;
 esac
 
+  case " $LIBOBJS " in
+  *" win32gai_strerror.$ac_objext "* ) ;;
+  *) LIBOBJS="$LIBOBJS win32gai_strerror.$ac_objext"
+ ;;
+esac
+
   case " $LIBOBJS " in
   *" win32getrusage.$ac_objext "* ) ;;
   *) LIBOBJS="$LIBOBJS win32getrusage.$ac_objext"
index 52fd7af446818b9c5f572b02d0cbbfe3a962e160..6e64ece11dad2c41d10d906579d1818d9707946e 100644 (file)
@@ -1885,6 +1885,7 @@ if test "$PORTNAME" = "win32"; then
   AC_LIBOBJ(win32env)
   AC_LIBOBJ(win32error)
   AC_LIBOBJ(win32fdatasync)
+  AC_LIBOBJ(win32gai_strerror)
   AC_LIBOBJ(win32getrusage)
   AC_LIBOBJ(win32link)
   AC_LIBOBJ(win32ntdll)
index 0c32c0f7b2ec34e2c50f32928ebaf9c02491fd68..f2b475df5e5e0122f98d8eb009d8e2445c5af4fd 100644 (file)
 #define ERROR PGERROR
 #endif
 
+/*
+ * We don't use the Windows gai_strerror[A] function because it is not
+ * thread-safe.  We define our own in src/port/win32gai_strerror.c.
+ */
+#undef gai_strerror
+
+extern const char *gai_strerror(int ecode);
+
 #endif                                                 /* WIN32_SYS_SOCKET_H */
index 69b30ab21b4caa3760713bcc6ce328d2cae91292..92b593e6ef3dccdbe181749c2b44d0e49ab7e999 100644 (file)
@@ -35,6 +35,7 @@ if host_system == 'windows'
     'win32error.c',
     'win32fdatasync.c',
     'win32fseek.c',
+    'win32gai_strerror.c',
     'win32getrusage.c',
     'win32link.c',
     'win32ntdll.c',
diff --git a/src/port/win32gai_strerror.c b/src/port/win32gai_strerror.c
new file mode 100644 (file)
index 0000000..5b47d17
--- /dev/null
@@ -0,0 +1,45 @@
+/*-------------------------------------------------------------------------
+ *
+ * win32gai_strerror.c
+ *       Thread-safe gai_strerror() for Windows.
+ *
+ * Portions Copyright (c) 2024, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ *    src/port/win32gai_strerror.c
+ *
+ *-------------------------------------------------------------------------
+ */
+
+#include <sys/socket.h>
+
+/*
+ * Windows has gai_strerrorA(), but it is not thread-safe so we avoid it.
+ *
+ * https://learn.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-gai_strerrora
+ */
+const char *
+gai_strerror(int errcode)
+{
+       switch (errcode)
+       {
+               case EAI_AGAIN:
+                       return "Temporary failure in name resolution";
+               case EAI_BADFLAGS:
+                       return "Bad value for ai_flags";
+               case EAI_FAIL:
+                       return "Non-recoverable failure in name resolution";
+               case EAI_FAMILY:
+                       return "ai_family not supported";
+               case EAI_MEMORY:
+                       return "Memory allocation failure";
+               case EAI_NONAME:
+                       return "Name or service not known";
+               case EAI_SERVICE:
+                       return "Servname not supported for ai_socktype";
+               case EAI_SOCKTYPE:
+                       return "ai_socktype not supported";
+               default:
+                       return "Unknown server error";
+       }
+}