Skip to content

Commit 5181a6e

Browse files
authored
gh-155336: Preserve resolver errors from gethostby*_r() (GH-155337)
1 parent c44dca0 commit 5181a6e

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
@@ -6041,7 +6041,7 @@ sock_decode_hostname(const char *name)
60416041

60426042
static PyObject *
60436043
gethost_common(socket_state *state, struct hostent *h, struct sockaddr *addr,
6044-
size_t alen, int af)
6044+
size_t alen, int af, int h_error)
60456045
{
60466046
char **pch;
60476047
PyObject *rtn_tuple = (PyObject *)NULL;
@@ -6052,7 +6052,7 @@ gethost_common(socket_state *state, struct hostent *h, struct sockaddr *addr,
60526052

60536053
if (h == NULL) {
60546054
/* Let's get real error message to return */
6055-
set_herror(state, h_errno);
6055+
set_herror(state, h_error);
60566056
return NULL;
60576057
}
60586058

@@ -6188,6 +6188,7 @@ static PyObject *
61886188
socket_gethostbyname_ex(PyObject *self, PyObject *args)
61896189
{
61906190
char *name;
6191+
int h_error;
61916192
struct hostent *h;
61926193
sock_addr_t addr;
61936194
struct sockaddr *sa;
@@ -6199,7 +6200,6 @@ socket_gethostbyname_ex(PyObject *self, PyObject *args)
61996200
#else
62006201
char buf[16384];
62016202
int buf_len = (sizeof buf) - 1;
6202-
int errnop;
62036203
#endif
62046204
#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
62056205
int result;
@@ -6218,14 +6218,14 @@ socket_gethostbyname_ex(PyObject *self, PyObject *args)
62186218
Py_BEGIN_ALLOW_THREADS
62196219
#ifdef HAVE_GETHOSTBYNAME_R
62206220
#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
6221-
gethostbyname_r(name, &hp_allocated, buf, buf_len,
6222-
&h, &errnop);
6221+
gethostbyname_r(name, &hp_allocated, buf, buf_len, &h, &h_error);
62236222
#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
6224-
h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
6223+
h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &h_error);
62256224
#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
62266225
memset((void *) &data, '\0', sizeof(data));
62276226
result = gethostbyname_r(name, &hp_allocated, &data);
62286227
h = (result != 0) ? NULL : &hp_allocated;
6228+
h_error = h_errno;
62296229
#endif
62306230
#else /* not HAVE_GETHOSTBYNAME_R */
62316231
#ifdef USE_GETHOSTBYNAME_LOCK
@@ -6235,6 +6235,7 @@ socket_gethostbyname_ex(PyObject *self, PyObject *args)
62356235
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
62366236
h = gethostbyname(name);
62376237
_Py_COMP_DIAG_POP
6238+
h_error = h_errno;
62386239
#endif /* HAVE_GETHOSTBYNAME_R */
62396240
Py_END_ALLOW_THREADS
62406241
/* Some C libraries would require addr.__ss_family instead of
@@ -6243,7 +6244,7 @@ socket_gethostbyname_ex(PyObject *self, PyObject *args)
62436244
access sa_family. */
62446245
sa = SAS2SA(&addr);
62456246
ret = gethost_common(state, h, SAS2SA(&addr), sizeof(addr),
6246-
sa->sa_family);
6247+
sa->sa_family, h_error);
62476248
#ifdef USE_GETHOSTBYNAME_LOCK
62486249
PyMutex_Unlock(&netdb_lock);
62496250
#endif
@@ -6282,7 +6283,6 @@ socket_gethostbyaddr(PyObject *self, PyObject *args)
62826283
to maintain this alignment. */
62836284
_Py_ALIGNED_DEF(8, char) buf[16384];
62846285
int buf_len = (sizeof buf) - 1;
6285-
int errnop;
62866286
#endif
62876287
#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
62886288
int result;
@@ -6291,6 +6291,7 @@ socket_gethostbyaddr(PyObject *self, PyObject *args)
62916291
const char *ap;
62926292
int al;
62936293
int af;
6294+
int h_error;
62946295

62956296
if (!PyArg_ParseTuple(args, "et:gethostbyaddr", "idna", &ip_num))
62966297
return NULL;
@@ -6323,16 +6324,14 @@ socket_gethostbyaddr(PyObject *self, PyObject *args)
63236324
Py_BEGIN_ALLOW_THREADS
63246325
#ifdef HAVE_GETHOSTBYNAME_R
63256326
#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
6326-
gethostbyaddr_r(ap, al, af,
6327-
&hp_allocated, buf, buf_len,
6328-
&h, &errnop);
6327+
gethostbyaddr_r(ap, al, af, &hp_allocated, buf, buf_len, &h, &h_error);
63296328
#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
6330-
h = gethostbyaddr_r(ap, al, af,
6331-
&hp_allocated, buf, buf_len, &errnop);
6329+
h = gethostbyaddr_r(ap, al, af, &hp_allocated, buf, buf_len, &h_error);
63326330
#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
63336331
memset((void *) &data, '\0', sizeof(data));
63346332
result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);
63356333
h = (result != 0) ? NULL : &hp_allocated;
6334+
h_error = h_errno;
63366335
#endif
63376336
#else /* not HAVE_GETHOSTBYNAME_R */
63386337
#ifdef USE_GETHOSTBYNAME_LOCK
@@ -6342,9 +6341,10 @@ socket_gethostbyaddr(PyObject *self, PyObject *args)
63426341
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
63436342
h = gethostbyaddr(ap, al, af);
63446343
_Py_COMP_DIAG_POP
6344+
h_error = h_errno;
63456345
#endif /* HAVE_GETHOSTBYNAME_R */
63466346
Py_END_ALLOW_THREADS
6347-
ret = gethost_common(state, h, SAS2SA(&addr), sizeof(addr), af);
6347+
ret = gethost_common(state, h, SAS2SA(&addr), sizeof(addr), af, h_error);
63486348
#ifdef USE_GETHOSTBYNAME_LOCK
63496349
PyMutex_Unlock(&netdb_lock);
63506350
#endif

0 commit comments

Comments
 (0)