8000 gh-100795: Don't call freeaddrinfo on failure. (GH-101252) · python/cpython@8126628 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8126628

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 <greg@krypto.org> Co-authored-by: Sergey G. Brester <github@sebres.de> Co-authored-by: Oleg Iarygin <dralife@yandex.ru>
1 parent e24c73e commit 8126628

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
@@ -1053,6 +1053,7 @@ setipaddr(const char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int
10531053
subsequent call to getaddrinfo() does not destroy the
10541054
outcome of the first call. */
10551055
if (error) {
1056+
res = NULL; // no-op, remind us that it is invalid; gh-100795
10561057
set_gaierror(error);
10571058
return -1;
10581059
}
@@ -1163,6 +1164,7 @@ setipaddr(const char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int
11631164
#endif
11641165
Py_END_ALLOW_THREADS
11651166
if (error) {
1167+
res = NULL; // no-op, remind us that it is invalid; gh-100795
11661168
set_gaierror(error);
11671169
return -1;
11681170
}
@@ -6514,6 +6516,7 @@ socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs)
65146516
error = getaddrinfo(hptr, pptr, &hints, &res0);
65156517
Py_END_ALLOW_THREADS
65166518
if (error) {
6519+
res0 = NULL; // gh-100795
65176520
set_gaierror(error);
65186521
goto err;
65196522
}
@@ -6608,6 +6611,7 @@ socket_getnameinfo(PyObject *self, PyObject *args)
66086611
error = getaddrinfo(hostp, pbuf, &hints, &res);
66096612
Py_END_ALLOW_THREADS
66106613
if (error) {
6614+
res = NULL; // gh-100795
66116615
set_gaierror(error);
66126616
goto fail;
66136617
}

0 commit comments

Comments
 (0)
0