From e185f727e093c560c8e1c06113fb88aac2cb2089 Mon Sep 17 00:00:00 2001 From: Max Bachmann Date: Fri, 2 Feb 2024 16:22:58 +0100 Subject: [PATCH 1/4] add support for AI_NUMERICSERV in getaddrinfo emulation --- Modules/addrinfo.h | 4 +++- Modules/getaddrinfo.c | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Modules/addrinfo.h b/Modules/addrinfo.h index 66e5a795f86f19..8c2cb3b4f3e727 100644 --- a/Modules/addrinfo.h +++ b/Modules/addrinfo.h @@ -77,6 +77,7 @@ #undef AI_PASSIVE #undef AI_CANONNAME #undef AI_NUMERICHOST +#undef AI_NUMERICSERV #undef AI_MASK #undef AI_ALL #undef AI_V4MAPPED_CFG @@ -88,8 +89,9 @@ #define AI_PASSIVE 0x00000001 /* get address to use bind() */ #define AI_CANONNAME 0x00000002 /* fill ai_canonname */ #define AI_NUMERICHOST 0x00000004 /* prevent name resolution */ +#define AI_NUMERICSERV 0x00000008 /* prevent service resolution */ /* valid flags for addrinfo */ -#define AI_MASK (AI_PASSIVE | AI_CANONNAME | AI_NUMERICHOST) +#define AI_MASK (AI_PASSIVE | AI_CANONNAME | AI_NUMERICHOST | AI_NUMERICSERV) #define AI_ALL 0x00000100 /* IPv6 and IPv4-mapped (with AI_V4MAPPED) */ #define AI_V4MAPPED_CFG 0x00000200 /* accept IPv4-mapped if kernel supports */ diff --git a/Modules/getaddrinfo.c b/Modules/getaddrinfo.c index 6fb6062a6520d9..ba698751aba094 100644 --- a/Modules/getaddrinfo.c +++ b/Modules/getaddrinfo.c @@ -351,6 +351,9 @@ getaddrinfo(const char*hostname, const char*servname, struct servent *sp; const char *proto; + if (ai->ai_flags & AI_NUMERICSERV) + return EAI_NONAME; + proto = NULL; switch (pai->ai_socktype) { case GAI_ANY: From eaa2540fbd2edfe17783a16901f2d3d3d72a5e38 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 2 Feb 2024 15:26:50 +0000 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2024-02-02-15-26-48.gh-issue-114917.sf0GuO.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2024-02-02-15-26-48.gh-issue-114917.sf0GuO.rst diff --git a/Misc/NEWS.d/next/Library/2024-02-02-15-26-48.gh-issue-114917.sf0GuO.rst b/Misc/NEWS.d/next/Library/2024-02-02-15-26-48.gh-issue-114917.sf0GuO.rst new file mode 100644 index 00000000000000..02aab3fd07e108 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-02-02-15-26-48.gh-issue-114917.sf0GuO.rst @@ -0,0 +1 @@ +Add support for AI_NUMERICSERV in getaddrinfo emulation From 104170194bd0d82b3d3e51a309426329a0d6e56a Mon Sep 17 00:00:00 2001 From: Max Bachmann Date: Tue, 11 Mar 2025 13:41:50 +0100 Subject: [PATCH 3/4] add test for socket.AI_NUMERICSERV --- Lib/test/test_socket.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index fc6e8593ae30bb..6572032c1d14bd 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -1662,8 +1662,11 @@ def testGetaddrinfo(self): # Issue #6697. self.assertRaises(UnicodeEncodeError, socket.getaddrinfo, 'localhost', '\uD800') - # Issue 17269: test workaround for OS X platform bug segfault if hasattr(socket, 'AI_NUMERICSERV'): + self.assertRaises(socket.gaierror, socket.getaddrinfo, "localhost", "http", + flags=socket.AI_NUMERICSERV) + + # Issue 17269: test workaround for OS X platform bug segfault try: # The arguments here are undefined and the call may succeed # or fail. All we care here is that it doesn't segfault. From 06c2ddc10f1e8503100c6f504404f1d0a434bbfc Mon Sep 17 00:00:00 2001 From: Max Bachmann Date: Wed, 12 Mar 2025 08:21:37 +0100 Subject: [PATCH 4/4] set res to NULL --- Modules/getaddrinfo.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Modules/getaddrinfo.c b/Modules/getaddrinfo.c index ba698751aba094..e42cd0f7cf6b6d 100644 --- a/Modules/getaddrinfo.c +++ b/Modules/getaddrinfo.c @@ -351,8 +351,9 @@ getaddrinfo(const char*hostname, const char*servname, struct servent *sp; const char *proto; - if (ai->ai_flags & AI_NUMERICSERV) - return EAI_NONAME; + if (ai->ai_flags & AI_NUMERICSERV) { + ERR(EAI_NONAME); + } proto = NULL; switch (pai->ai_socktype) {