Skip to content

Commit 5964b12

Browse files
miss-islingtongpsheadsebresarhadthedev
authored
gh-100795: Don't call freeaddrinfo on failure. (GH-101252)
When getaddrinfo returns an error, the output pointer is in an unknown state Don't call freeaddrinfo on it. See the issue for discussion and details with links to reasoning. _Most_ libc getaddrinfo implementations never modify the output pointer unless they are returning success. (cherry picked from commit b724ac2) Co-authored-by: Gregory P. Smith <[email protected]> Co-authored-by: Sergey G. Brester <[email protected]> Co-authored-by: Oleg Iarygin <[email protected]>
1 parent 4cf4169 commit 5964b12

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Avoid potential unexpected ``freeaddrinfo`` call (double free) in :mod:`socket`
2+
when when a libc ``getaddrinfo()`` implementation leaves garbage in an output
3+
pointer when returning an error. Original patch by Sergey G. Brester.

Modules/socketmodule.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,7 @@ setipaddr(const char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int
10781078
subsequent call to getaddrinfo() does not destroy the
10791079
outcome of the first call. */
10801080
if (error) {
1081+
res = NULL; // no-op, remind us that it is invalid; gh-100795
10811082
set_gaierror(error);
10821083
return -1;
10831084
}
@@ -1188,6 +1189,7 @@ setipaddr(const char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int
11881189
#endif
11891190
Py_END_ALLOW_THREADS
11901191
if (error) {
1192+
res = NULL; // no-op, remind us that it is invalid; gh-100795
11911193
set_gaierror(error);
11921194
return -1;
11931195
}
@@ -6608,6 +6610,7 @@ socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs)
66086610
error = getaddrinfo(hptr, pptr, &hints, &res0);
66096611
Py_END_ALLOW_THREADS
66106612
if (error) {
6613+
res0 = NULL; // gh-100795
66116614
set_gaierror(error);
66126615
goto err;
66136616
}
@@ -6704,6 +6707,7 @@ socket_getnameinfo(PyObject *self, PyObject *args)
67046707
error = getaddrinfo(hostp, pbuf, &hints, &res);
67056708
Py_END_ALLOW_THREADS
67066709
if (error) {
6710+
res = NULL; // gh-100795
67076711
set_gaierror(error);
67086712
goto fail;
67096713
}

0 commit comments

Comments
 (0)