From 08ce2e17276fa363da1a69cf6fc834cbe81f48cc Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Wed, 21 Oct 2026 12:02:50 -0700 Subject: [PATCH] gh-159739: Propagate fallback getaddrinfo allocation failures (GH-159984) On builds using CPython's fallback `getaddrinfo()`, an allocation failure while constructing address results can return success with a null result, and the canonical-name path can ignore a helper error. `socket.getaddrinfo()` can then return an empty list, other callers can crash when they dereference the null result, and partially built address lists may leak. Set `EAI_MEMORY`, propagate helper errors, and keep the partial list available for cleanup. (cherry picked from commit 7c39e8fabbb5735dffdf97021167aa22bd55e30c) Co-authored-by: Jelle Zijlstra --- ...26-10-20-00-00-00.gh-issue-159739.9ef89c.rst | 2 ++ Modules/getaddrinfo.c | 17 +++++++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-10-20-00-00-00.gh-issue-159739.9ef89c.rst diff --git a/Misc/NEWS.d/next/Library/2026-10-20-00-00-00.gh-issue-159739.9ef89c.rst b/Misc/NEWS.d/next/Library/2026-10-20-00-00-00.gh-issue-159739.9ef89c.rst new file mode 100644 index 00000000000000..1afbadc1cf4e91 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-20-00-00-00.gh-issue-159739.9ef89c.rst @@ -0,0 +1,2 @@ +Fix allocation error handling in the fallback implementation of +``getaddrinfo()``. diff --git a/Modules/getaddrinfo.c b/Modules/getaddrinfo.c index de6f0bf57b5c31..311632879855a0 100644 --- a/Modules/getaddrinfo.c +++ b/Modules/getaddrinfo.c @@ -171,7 +171,10 @@ if (pai->ai_flags & AI_CANONNAME) {\ char *p;\ if (((ai) = (struct addrinfo *)malloc(sizeof(struct addrinfo) +\ ((gai_afd)->a_socklen)))\ - == NULL) goto free;\ + == NULL) {\ + error = EAI_MEMORY;\ + goto free;\ + }\ memcpy(ai, pai, sizeof(struct addrinfo));\ (ai)->ai_addr = (struct sockaddr *)((ai) + 1);\ memset((ai)->ai_addr, 0, (gai_afd)->a_socklen);\ @@ -186,7 +189,10 @@ if (pai->ai_flags & AI_CANONNAME) {\ char *p;\ if (((ai) = (struct addrinfo *)malloc(sizeof(struct addrinfo) +\ ((gai_afd)->a_socklen)))\ - == NULL) goto free;\ + == NULL) {\ + error = EAI_MEMORY;\ + goto free;\ + }\ memcpy(ai, pai, sizeof(struct addrinfo));\ (ai)->ai_addr = (struct sockaddr *)((ai) + 1);\ memset((ai)->ai_addr, 0, (gai_afd)->a_socklen);\ @@ -413,8 +419,8 @@ getaddrinfo(const char*hostname, const char*servname, */ } cur = cur->ai_next; + top = sentinel.ai_next; } - top = sentinel.ai_next; if (top) goto good; else @@ -463,7 +469,10 @@ getaddrinfo(const char*hostname, const char*servname, * translation function, and it looks strange * that we do addr->name translation here. */ - get_name(pton, &gai_afdl[i], &top, pton, pai, port); + error = get_name(pton, &gai_afdl[i], &top, pton, pai, port); + if (error != SUCCESS) { + goto free; + } goto good; } else ERR(EAI_FAMILY); /*xxx*/