Skip to content

Commit 18dd3f0

Browse files
[3.15] gh-159739: Propagate fallback getaddrinfo allocation failures (GH-159984) (#160099)
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 7c39e8f) Co-authored-by: Jelle Zijlstra <jelle@openai.com>
1 parent 2eddd80 commit 18dd3f0

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix allocation error handling in the fallback implementation of
2+
``getaddrinfo()``.

‎Modules/getaddrinfo.c‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,10 @@ if (pai->ai_flags & AI_CANONNAME) {\
171171
char *p;\
172172
if (((ai) = (struct addrinfo *)malloc(sizeof(struct addrinfo) +\
173173
((gai_afd)->a_socklen)))\
174-
== NULL) goto free;\
174+
== NULL) {\
175+
error = EAI_MEMORY;\
176+
goto free;\
177+
}\
175178
memcpy(ai, pai, sizeof(struct addrinfo));\
176179
(ai)->ai_addr = (struct sockaddr *)((ai) + 1);\
177180
memset((ai)->ai_addr, 0, (gai_afd)->a_socklen);\
@@ -186,7 +189,10 @@ if (pai->ai_flags & AI_CANONNAME) {\
186189
char *p;\
187190
if (((ai) = (struct addrinfo *)malloc(sizeof(struct addrinfo) +\
188191
((gai_afd)->a_socklen)))\
189-
== NULL) goto free;\
192+
== NULL) {\
193+
error = EAI_MEMORY;\
194+
goto free;\
195+
}\
190196
memcpy(ai, pai, sizeof(struct addrinfo));\
191197
(ai)->ai_addr = (struct sockaddr *)((ai) + 1);\
192198
memset((ai)->ai_addr, 0, (gai_afd)->a_socklen);\
@@ -413,8 +419,8 @@ getaddrinfo(const char*hostname, const char*servname,
413419
*/
414420
}
415421
cur = cur->ai_next;
422+
top = sentinel.ai_next;
416423
}
417-
top = sentinel.ai_next;
418424
if (top)
419425
goto good;
420426
else
@@ -463,7 +469,10 @@ getaddrinfo(const char*hostname, const char*servname,
463469
* translation function, and it looks strange
464470
* that we do addr->name translation here.
465471
*/
466-
get_name(pton, &gai_afdl[i], &top, pton, pai, port);
472+
error = get_name(pton, &gai_afdl[i], &top, pton, pai, port);
473+
if (error != SUCCESS) {
474+
goto free;
475+
}
467476
goto good;
468477
} else
469478
ERR(EAI_FAMILY); /*xxx*/

0 commit comments

Comments
 (0)