From 7c1fa06ad8ce914a8206844f4138d36f832988f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Kul=C3=ADk?= Date: Fri, 7 Aug 2026 13:59:09 +0200 Subject: [PATCH 1/3] fix error handling in socket.gethostbyaddr and socket.gethostbyname_ex functions --- Modules/socketmodule.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index 73ae1c942daba48..d94252a1c412d25 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -6041,7 +6041,7 @@ sock_decode_hostname(const char *name) static PyObject * gethost_common(socket_state *state, struct hostent *h, struct sockaddr *addr, - size_t alen, int af) + size_t alen, int af, int h_error) { char **pch; PyObject *rtn_tuple = (PyObject *)NULL; @@ -6052,7 +6052,7 @@ gethost_common(socket_state *state, struct hostent *h, struct sockaddr *addr, if (h == NULL) { /* Let's get real error message to return */ - set_herror(state, h_errno); + set_herror(state, h_error); return NULL; } @@ -6188,6 +6188,7 @@ static PyObject * socket_gethostbyname_ex(PyObject *self, PyObject *args) { char *name; + int h_error; struct hostent *h; sock_addr_t addr; struct sockaddr *sa; @@ -6220,12 +6221,15 @@ socket_gethostbyname_ex(PyObject *self, PyObject *args) #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) gethostbyname_r(name, &hp_allocated, buf, buf_len, &h, &errnop); + h_error = errnop; #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop); + h_error = errnop; #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ memset((void *) &data, '\0', sizeof(data)); result = gethostbyname_r(name, &hp_allocated, &data); h = (result != 0) ? NULL : &hp_allocated; + h_error = h_errno; #endif #else /* not HAVE_GETHOSTBYNAME_R */ #ifdef USE_GETHOSTBYNAME_LOCK @@ -6235,6 +6239,7 @@ socket_gethostbyname_ex(PyObject *self, PyObject *args) _Py_COMP_DIAG_IGNORE_DEPR_DECLS h = gethostbyname(name); _Py_COMP_DIAG_POP + h_error = h_errno; #endif /* HAVE_GETHOSTBYNAME_R */ Py_END_ALLOW_THREADS /* Some C libraries would require addr.__ss_family instead of @@ -6243,7 +6248,7 @@ socket_gethostbyname_ex(PyObject *self, PyObject *args) access sa_family. */ sa = SAS2SA(&addr); ret = gethost_common(state, h, SAS2SA(&addr), sizeof(addr), - sa->sa_family); + sa->sa_family, h_error); #ifdef USE_GETHOSTBYNAME_LOCK PyMutex_Unlock(&netdb_lock); #endif @@ -6291,6 +6296,7 @@ socket_gethostbyaddr(PyObject *self, PyObject *args) const char *ap; int al; int af; + int h_error; if (!PyArg_ParseTuple(args, "et:gethostbyaddr", "idna", &ip_num)) return NULL; @@ -6326,13 +6332,16 @@ socket_gethostbyaddr(PyObject *self, PyObject *args) gethostbyaddr_r(ap, al, af, &hp_allocated, buf, buf_len, &h, &errnop); + h_error = errnop; #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) h = gethostbyaddr_r(ap, al, af, &hp_allocated, buf, buf_len, &errnop); + h_error = errnop; #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ memset((void *) &data, '\0', sizeof(data)); result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data); h = (result != 0) ? NULL : &hp_allocated; + h_error = h_errno; #endif #else /* not HAVE_GETHOSTBYNAME_R */ #ifdef USE_GETHOSTBYNAME_LOCK @@ -6342,9 +6351,10 @@ socket_gethostbyaddr(PyObject *self, PyObject *args) _Py_COMP_DIAG_IGNORE_DEPR_DECLS h = gethostbyaddr(ap, al, af); _Py_COMP_DIAG_POP + h_error = h_errno; #endif /* HAVE_GETHOSTBYNAME_R */ Py_END_ALLOW_THREADS - ret = gethost_common(state, h, SAS2SA(&addr), sizeof(addr), af); + ret = gethost_common(state, h, SAS2SA(&addr), sizeof(addr), af, h_error); #ifdef USE_GETHOSTBYNAME_LOCK PyMutex_Unlock(&netdb_lock); #endif From e8599611cea84d4ea6c2d35ae770a9d51d64376b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Kul=C3=ADk?= Date: Fri, 7 Aug 2026 14:01:03 +0200 Subject: [PATCH 2/3] formatting changes --- Modules/socketmodule.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index d94252a1c412d25..d63fa8e98197473 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -6219,8 +6219,7 @@ socket_gethostbyname_ex(PyObject *self, PyObject *args) Py_BEGIN_ALLOW_THREADS #ifdef HAVE_GETHOSTBYNAME_R #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) - gethostbyname_r(name, &hp_allocated, buf, buf_len, - &h, &errnop); + gethostbyname_r(name, &hp_allocated, buf, buf_len, &h, &errnop); h_error = errnop; #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop); @@ -6329,13 +6328,10 @@ socket_gethostbyaddr(PyObject *self, PyObject *args) Py_BEGIN_ALLOW_THREADS #ifdef HAVE_GETHOSTBYNAME_R #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) - gethostbyaddr_r(ap, al, af, - &hp_allocated, buf, buf_len, - &h, &errnop); + gethostbyaddr_r(ap, al, af, &hp_allocated, buf, buf_len, &h, &errnop); h_error = errnop; #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) - h = gethostbyaddr_r(ap, al, af, - &hp_allocated, buf, buf_len, &errnop); + h = gethostbyaddr_r(ap, al, af, &hp_allocated, buf, buf_len, &errnop); h_error = errnop; #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ memset((void *) &data, '\0', sizeof(data)); From 807a6a1fa489c4c12817315a7bb242fbfdb6d999 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Kul=C3=ADk?= Date: Fri, 7 Aug 2026 14:30:51 +0200 Subject: [PATCH 3/3] clean it up a little bit --- Modules/socketmodule.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c index d63fa8e98197473..f9c77c631b5d2af 100644 --- a/Modules/socketmodule.c +++ b/Modules/socketmodule.c @@ -6200,7 +6200,6 @@ socket_gethostbyname_ex(PyObject *self, PyObject *args) #else char buf[16384]; int buf_len = (sizeof buf) - 1; - int errnop; #endif #ifdef HAVE_GETHOSTBYNAME_R_3_ARG int result; @@ -6219,11 +6218,9 @@ socket_gethostbyname_ex(PyObject *self, PyObject *args) Py_BEGIN_ALLOW_THREADS #ifdef HAVE_GETHOSTBYNAME_R #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) - gethostbyname_r(name, &hp_allocated, buf, buf_len, &h, &errnop); - h_error = errnop; + gethostbyname_r(name, &hp_allocated, buf, buf_len, &h, &h_error); #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) - h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop); - h_error = errnop; + h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &h_error); #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ memset((void *) &data, '\0', sizeof(data)); result = gethostbyname_r(name, &hp_allocated, &data); @@ -6286,7 +6283,6 @@ socket_gethostbyaddr(PyObject *self, PyObject *args) to maintain this alignment. */ _Py_ALIGNED_DEF(8, char) buf[16384]; int buf_len = (sizeof buf) - 1; - int errnop; #endif #ifdef HAVE_GETHOSTBYNAME_R_3_ARG int result; @@ -6328,11 +6324,9 @@ socket_gethostbyaddr(PyObject *self, PyObject *args) Py_BEGIN_ALLOW_THREADS #ifdef HAVE_GETHOSTBYNAME_R #if defined(HAVE_GETHOSTBYNAME_R_6_ARG) - gethostbyaddr_r(ap, al, af, &hp_allocated, buf, buf_len, &h, &errnop); - h_error = errnop; + gethostbyaddr_r(ap, al, af, &hp_allocated, buf, buf_len, &h, &h_error); #elif defined(HAVE_GETHOSTBYNAME_R_5_ARG) - h = gethostbyaddr_r(ap, al, af, &hp_allocated, buf, buf_len, &errnop); - h_error = errnop; + h = gethostbyaddr_r(ap, al, af, &hp_allocated, buf, buf_len, &h_error); #else /* HAVE_GETHOSTBYNAME_R_3_ARG */ memset((void *) &data, '\0', sizeof(data)); result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);