Skip to content

Commit bc8d3cf

Browse files
kulikjakmiss-islington
authored andcommitted
gh-155336: Preserve resolver errors from gethostby*_r() (GH-155337)
(cherry picked from commit 5181a6e) Co-authored-by: Jakub Kulík <Kulikjak@gmail.com>
1 parent 10702eb commit bc8d3cf

2 files changed

Lines changed: 16 additions & 14 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix error handling in :func:`socket.gethostbyaddr` and
2+
:func:`socket.gethostbyname_ex` when hostname resolution fails.

Modules/socketmodule.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6021,7 +6021,7 @@ sock_decode_hostname(const char *name)
60216021

60226022
static PyObject *
60236023
gethost_common(socket_state *state, struct hostent *h, struct sockaddr *addr,
6024-
size_t alen, int af)
6024+
size_t alen, int af, int h_error)
60256025
{
60266026
char **pch;
60276027
PyObject *rtn_tuple = (PyObject *)NULL;
@@ -6032,7 +6032,7 @@ gethost_common(socket_state *state, struct hostent *h, struct sockaddr *addr,
60326032

60336033
if (h == NULL) {
60346034
/* Let's get real error message to return */
6035-
set_herror(state, h_errno);
6035+
set_herror(state, h_error);
60366036
return NULL;
60376037
}
60386038

@@ -6168,6 +6168,7 @@ static PyObject *
61686168
socket_gethostbyname_ex(PyObject *self, PyObject *args)
61696169
{
61706170
char *name;
6171+
int h_error;
61716172
struct hostent *h;
61726173
sock_addr_t addr;
61736174
struct sockaddr *sa;
@@ -6179,7 +6180,6 @@ socket_gethostbyname_ex(PyObject *self, PyObject *args)
61796180
#else
61806181
char buf[16384];
61816182
int buf_len = (sizeof buf) - 1;
6182-
int errnop;
61836183
#endif
61846184
#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
61856185
int result;
@@ -6198,14 +6198,14 @@ socket_gethostbyname_ex(PyObject *self, PyObject *args)
61986198
Py_BEGIN_ALLOW_THREADS
61996199
#ifdef HAVE_GETHOSTBYNAME_R
62006200
#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
6201-
gethostbyname_r(name, &hp_allocated, buf, buf_len,
6202-
&h, &errnop);
6201+
gethostbyname_r(name, &hp_allocated, buf, buf_len, &h, &h_error);
62036202
#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
6204-
h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
6203+
h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &h_error);
62056204
#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
62066205
memset((void *) &data, '\0', sizeof(data));
62076206
result = gethostbyname_r(name, &hp_allocated, &data);
62086207
h = (result != 0) ? NULL : &hp_allocated;
6208+
h_error = h_errno;
62096209
#endif
62106210
#else /* not HAVE_GETHOSTBYNAME_R */
62116211
#ifdef USE_GETHOSTBYNAME_LOCK
@@ -6215,6 +6215,7 @@ socket_gethostbyname_ex(PyObject *self, PyObject *args)
62156215
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
62166216
h = gethostbyname(name);
62176217
_Py_COMP_DIAG_POP
6218+
h_error = h_errno;
62186219
#endif /* HAVE_GETHOSTBYNAME_R */
62196220
Py_END_ALLOW_THREADS
62206221
/* Some C libraries would require addr.__ss_family instead of
@@ -6223,7 +6224,7 @@ socket_gethostbyname_ex(PyObject *self, PyObject *args)
62236224
access sa_family. */
62246225
sa = SAS2SA(&addr);
62256226
ret = gethost_common(state, h, SAS2SA(&addr), sizeof(addr),
6226-
sa->sa_family);
6227+
sa->sa_family, h_error);
62276228
#ifdef USE_GETHOSTBYNAME_LOCK
62286229
PyThread_release_lock(netdb_lock);
62296230
#endif
@@ -6262,7 +6263,6 @@ socket_gethostbyaddr(PyObject *self, PyObject *args)
62626263
to maintain this alignment. */
62636264
char buf[16384] Py_ALIGNED(8);
62646265
int buf_len = (sizeof buf) - 1;
6265-
int errnop;
62666266
#endif
62676267
#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
62686268
int result;
@@ -6271,6 +6271,7 @@ socket_gethostbyaddr(PyObject *self, PyObject *args)
62716271
const char *ap;
62726272
int al;
62736273
int af;
6274+
int h_error;
62746275

62756276
if (!PyArg_ParseTuple(args, "et:gethostbyaddr", "idna", &ip_num))
62766277
return NULL;
@@ -6303,16 +6304,14 @@ socket_gethostbyaddr(PyObject *self, PyObject *args)
63036304
Py_BEGIN_ALLOW_THREADS
63046305
#ifdef HAVE_GETHOSTBYNAME_R
63056306
#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
6306-
gethostbyaddr_r(ap, al, af,
6307-
&hp_allocated, buf, buf_len,
6308-
&h, &errnop);
6307+
gethostbyaddr_r(ap, al, af, &hp_allocated, buf, buf_len, &h, &h_error);
63096308
#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
6310-
h = gethostbyaddr_r(ap, al, af,
6311-
&hp_allocated, buf, buf_len, &errnop);
6309+
h = gethostbyaddr_r(ap, al, af, &hp_allocated, buf, buf_len, &h_error);
63126310
#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
63136311
memset((void *) &data, '\0', sizeof(data));
63146312
result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);
63156313
h = (result != 0) ? NULL : &hp_allocated;
6314+
h_error = h_errno;
63166315
#endif
63176316
#else /* not HAVE_GETHOSTBYNAME_R */
63186317
#ifdef USE_GETHOSTBYNAME_LOCK
@@ -6322,9 +6321,10 @@ socket_gethostbyaddr(PyObject *self, PyObject *args)
63226321
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
63236322
h = gethostbyaddr(ap, al, af);
63246323
_Py_COMP_DIAG_POP
6324+
h_error = h_errno;
63256325
#endif /* HAVE_GETHOSTBYNAME_R */
63266326
Py_END_ALLOW_THREADS
6327-
ret = gethost_common(state, h, SAS2SA(&addr), sizeof(addr), af);
6327+
ret = gethost_common(state, h, SAS2SA(&addr), sizeof(addr), af, h_error);
63286328
#ifdef USE_GETHOSTBYNAME_LOCK
63296329
PyThread_release_lock(netdb_lock);
63306330
#endif

0 commit comments

Comments
 (0)