diff --git a/Misc/NEWS.d/next/Library/2023-01-21-16-50-22.gh-issue-100795.NPMZf7.rst b/Misc/NEWS.d/next/Library/2023-01-21-16-50-22.gh-issue-100795.NPMZf7.rst new file mode 100644 index 00000000000000..beec5c9e57f647 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-01-21-16-50-22.gh-issue-100795.NPMZf7.rst @@ -0,0 +1,2 @@ +Avoid unexpected ``freeaddrinfo`` when :meth:`socket.socket.getaddrinfo` +fails. Patch by Sergey G. Brester. diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 6a9ac2ceb734e8..f2fd2bca2bff0d 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -6514,6 +6514,7 @@ socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs) error = getaddrinfo(hptr, pptr, &hints, &res0); Py_END_ALLOW_THREADS if (error) { + res0 = NULL; /* avoid unexpected free if res0 becomes not NULL */ set_gaierror(error); goto err; } @@ -6608,6 +6609,7 @@ socket_getnameinfo(PyObject *self, PyObject *args) error = getaddrinfo(hostp, pbuf, &hints, &res); Py_END_ALLOW_THREADS if (error) { + res = NULL; /* avoid unexpected free if res becomes not NULL */ set_gaierror(error); goto fail; }