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

Skip to content

Commit b724ac2

Browse files
gpsheadsebresarhadthedev
authored
pythongh-100795: Don't call freeaddrinfo on failure. (python#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. Co-authored-by: Sergey G. Brester <github@sebres.de> Co-authored-by: Oleg Iarygin <dralife@yandex.ru>
1 parent bd79039 commit b724ac2

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
@@ -1085,6 +1085,7 @@ setipaddr(const char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int
10851085
subsequent call to getaddrinfo() does not destroy the
10861086
outcome of the first call. */
10871087
if (error) {
1088+
res = NULL; // no-op, remind us that it is invalid; gh-100795
10881089
set_gaierror(error);
10891090
return -1;
10901091
}
@@ -1195,6 +1196,7 @@ setipaddr(const char *name, struct sockaddr *addr_ret, size_t addr_ret_size, int
11951196
#endif
11961197
Py_END_ALLOW_THREADS
11971198
if (error) {
1199+
res = NULL; // no-op, remind us that it is invalid; gh-100795
11981200
set_gaierror(error);
11991201
return -1;
12001202
}
@@ -6719,6 +6721,7 @@ socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs)
67196721
error = getaddrinfo(hptr, pptr, &hints, &res0);
67206722
Py_END_ALLOW_THREADS
67216723
if (error) {
6724+
res0 = NULL; // gh-100795
67226725
set_gaierror(error);
67236726
goto err;
67246727
}
@@ -6815,6 +6818,7 @@ socket_getnameinfo(PyObject *self, PyObject *args)
68156818
error = getaddrinfo(hostp, pbuf, &hints, &res);
68166819
Py_END_ALLOW_THREADS
68176820
if (error) {
6821+
res = NULL; // gh-100795
68186822
set_gaierror(error);
68196823
goto fail;
68206824
}

0 commit comments

Comments
 (0)
0