https://github.com/python/cpython/commit/ffecc6a41a6d884a334ef74a8249e2ba5d69fb32
commit: ffecc6a41a6d884a334ef74a8249e2ba5d69fb32
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: JelleZijlstra <[email protected]>
date: 2026-10-22T07:43:23-07:00
summary:

[3.14] gh-159739: Propagate fallback getaddrinfo allocation failures 
(GH-159984) (#160100)



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 <[email protected]>

files:
A Misc/NEWS.d/next/Library/2026-10-20-00-00-00.gh-issue-159739.9ef89c.rst
M Modules/getaddrinfo.c

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*/

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to