gai_strerror() is not thread-safe on Windows
Thomas Munro <thomas.munro@gmail.com>
From: Thomas Munro <thomas.munro@gmail.com>
To: pgsql-hackers <pgsql-hackers@postgresql.org>
Date: 2023-12-04T03:21:24Z
Lists: pgsql-hackers
Attachments
- 0001-Fix-gai_strerror-thread-safety-on-Windows.patch (text/x-patch) patch 0001
Hi, Commit 5579388d removed a bunch of dead code, formerly needed for old systems that lacked getaddrinfo() in the early days of IPv6. We already used the system getaddrinfo() via either configure-time tests (Unix) or runtime tests (Windows using attempt-to-find-with-dlsym that always succeeded on modern systems), so no modern system needed the fallback code, except for one small detail: getaddrinfo() has a companion function to spit out human readable error messages, and although Windows has that too, it's not thread safe[1]. libpq shouldn't call it, or else an unlucky multi-threaded program might see an error message messed up by another thread. Here's a patch to put that bit back. It's simpler than before: the original replacement had a bunch of #ifdefs for various historical reasons, but now we can just handle the 8 documented EAI errors on Windows. Noticed while wondering why the list of symbols reported in bug #18219 didn't include gai_strerrorA. That turned out to be because it is static inline in ws2tcpip.h, and its definition set alarm bells ringing. Avoid. [1] https://learn.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-getaddrinfo
Commits
-
Fix gai_strerror() thread-safety on Windows.
- 0460e4ecc0fa 16.3 landed
- 65f438471b76 17.0 landed
-
Update comment, generation mem contexts have a "keeper" block
- 376c216138c7 17.0 cited
-
Remove replacement code for getaddrinfo.
- 5579388d2dda 16.0 cited