11.11.2025 23:23, Daniel Gustafsson пишет:
On 10 Nov 2025, at 06:14, Sergey Tatarintsev <[email protected]>
wrote:
I'm trying to use pg_getaddrinfo_all() with NULL hints, but got segfault.
According to man(3) getaddrinfo, hints may be passed as NULL. In this case
af_family
is equivalent to AF_UNSPEC.
Right, but pg_getaddrinfo_all isn't getaddrinfo and doesn't claim to be. Since
pg_getaddrinfo_all can return AF_UNIX as opposed to getaddrinfo, it's not clear
why hints == NULL should mean ipv4|ipv6 here.
Accepting NULL or also (subtly) breaks the API for pg_freeaddrinfo_all which is
defined to take ai_family from the hints passed to pg_getaddrinfo_all. Now,
reading the code makes it obvious that it will work anyways, but at the very
least a patch to accept a NULL hint should update the function comment for
pg_freeaddrinfo_all.
--
Daniel Gustafsson
Daniel, thanks for review!
I added a comment to pg_freeaddrinfo_all.
I don't think it made sense to comment that hint_ai_family simply
shouldn't be equal to AF_UNIX,
so I specified that if the original hints were NULL, hint_ai_family
should be equal to AF_UNSPEC.
--
With best regards,
Sergey Tatarintsev,
PostgresPro
From 4fe0233cdef56658010643a2acb4c88aab1ebf83 Mon Sep 17 00:00:00 2001
From: Sergey Tatarintsev <[email protected]>
Date: Mon, 10 Nov 2025 11:41:12 +0700
Subject: [PATCH v2] Ability to pass NULL as a hintp to pg_getaddrinfo_all()
---
src/common/ip.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/src/common/ip.c b/src/common/ip.c
index 0e7897a5c8f..b9b8cec0873 100644
--- a/src/common/ip.c
+++ b/src/common/ip.c
@@ -58,7 +58,8 @@ pg_getaddrinfo_all(const char *hostname, const char *servname,
/* not all versions of getaddrinfo() zero *result on failure */
*result = NULL;
- if (hintp->ai_family == AF_UNIX)
+ /* Pass NULL to hintp is equivalent to ai_family=AF_UNSPEC */
+ if (hintp && hintp->ai_family == AF_UNIX)
return getaddrinfo_unix(servname, hintp, result);
/* NULL has special meaning to getaddrinfo(). */
@@ -77,6 +78,7 @@ pg_getaddrinfo_all(const char *hostname, const char *servname,
* getaddrinfo() routine or our own getaddrinfo_unix() routine. Some versions
* of getaddrinfo() might be willing to return AF_UNIX addresses, so it's
* not safe to look at ai_family in the addrinfo itself.
+ * If original hint structure was NULL, hint_ai_family must be equal to AF_UNSPEC.
*/
void
pg_freeaddrinfo_all(int hint_ai_family, struct addrinfo *ai)
--
2.43.0