Skip to content

Commit 014cc92

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 a1ab3b0 commit 014cc92

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

60346034
static PyObject *
60356035
gethost_common(socket_state *state, struct hostent *h, struct sockaddr *addr,
6036-
size_t alen, int af)
6036+
size_t alen, int af, int h_error)
60376037
{
60386038
char **pch;
60396039
PyObject *rtn_tuple = (PyObject *)NULL;
@@ -6044,7 +6044,7 @@ gethost_common(socket_state *state, struct hostent *h, struct sockaddr *addr,
60446044

60456045
if (h == NULL) {
60466046
/* Let's get real error message to return */
6047-
set_herror(state, h_errno);
6047+
set_herror(state, h_error);
60486048
return NULL;
60496049
}
60506050

@@ -6180,6 +6180,7 @@ static PyObject *
61806180
socket_gethostbyname_ex(PyObject *self, PyObject *args)
61816181
{
61826182
char *name;
6183+
int h_error;
61836184
struct hostent *h;
61846185
sock_addr_t addr;
61856186
struct sockaddr *sa;
@@ -6191,7 +6192,6 @@ socket_gethostbyname_ex(PyObject *self, PyObject *args)
61916192
#else
61926193
char buf[16384];
61936194
int buf_len = (sizeof buf) - 1;
6194-
int errnop;
61956195
#endif
61966196
#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
61976197
int result;
@@ -6210,14 +6210,14 @@ socket_gethostbyname_ex(PyObject *self, PyObject *args)
62106210
Py_BEGIN_ALLOW_THREADS
62116211
#ifdef HAVE_GETHOSTBYNAME_R
62126212
#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
6213-
gethostbyname_r(name, &hp_allocated, buf, buf_len,
6214-
&h, &errnop);
6213+
gethostbyname_r(name, &hp_allocated, buf, buf_len, &h, &h_error);
62156214
#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
6216-
h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
6215+
h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &h_error);
62176216
#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
62186217
memset((void *) &data, '\0', sizeof(data));
62196218
result = gethostbyname_r(name, &hp_allocated, &data);
62206219
h = (result != 0) ? NULL : &hp_allocated;
6220+
h_error = h_errno;
62216221
#endif
62226222
#else /* not HAVE_GETHOSTBYNAME_R */
62236223
#ifdef USE_GETHOSTBYNAME_LOCK
@@ -6227,6 +6227,7 @@ socket_gethostbyname_ex(PyObject *self, PyObject *args)
62276227
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
62286228
h = gethostbyname(name);
62296229
_Py_COMP_DIAG_POP
6230+
h_error = h_errno;
62306231
#endif /* HAVE_GETHOSTBYNAME_R */
62316232
Py_END_ALLOW_THREADS
62326233
/* Some C libraries would require addr.__ss_family instead of
@@ -6235,7 +6236,7 @@ socket_gethostbyname_ex(PyObject *self, PyObject *args)
62356236
access sa_family. */
62366237
sa = SAS2SA(&addr);
62376238
ret = gethost_common(state, h, SAS2SA(&addr), sizeof(addr),
6238-
sa->sa_family);
6239+
sa->sa_family, h_error);
62396240
#ifdef USE_GETHOSTBYNAME_LOCK
62406241
PyThread_release_lock(netdb_lock);
62416242
#endif
@@ -6274,7 +6275,6 @@ socket_gethostbyaddr(PyObject *self, PyObject *args)
62746275
to maintain this alignment. */
62756276
_Py_ALIGNED_DEF(8, char) buf[16384];
62766277
int buf_len = (sizeof buf) - 1;
6277-
int errnop;
62786278
#endif
62796279
#ifdef HAVE_GETHOSTBYNAME_R_3_ARG
62806280
int result;
@@ -6283,6 +6283,7 @@ socket_gethostbyaddr(PyObject *self, PyObject *args)
62836283
const char *ap;
62846284
int al;
62856285
int af;
6286+
int h_error;
62866287

62876288
if (!PyArg_ParseTuple(args, "et:gethostbyaddr", "idna", &ip_num))
62886289
return NULL;
@@ -6315,16 +6316,14 @@ socket_gethostbyaddr(PyObject *self, PyObject *args)
63156316
Py_BEGIN_ALLOW_THREADS
63166317
#ifdef HAVE_GETHOSTBYNAME_R
63176318
#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
6318-
gethostbyaddr_r(ap, al, af,
6319-
&hp_allocated, buf, buf_len,
6320-
&h, &errnop);
6319+
gethostbyaddr_r(ap, al, af, &hp_allocated, buf, buf_len, &h, &h_error);
63216320
#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
6322-
h = gethostbyaddr_r(ap, al, af,
6323-
&hp_allocated, buf, buf_len, &errnop);
6321+
h = gethostbyaddr_r(ap, al, af, &hp_allocated, buf, buf_len, &h_error);
63246322
#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
63256323
memset((void *) &data, '\0', sizeof(data));
63266324
result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);
63276325
h = (result != 0) ? NULL : &hp_allocated;
6326+
h_error = h_errno;
63286327
#endif
63296328
#else /* not HAVE_GETHOSTBYNAME_R */
63306329
#ifdef USE_GETHOSTBYNAME_LOCK
@@ -6334,9 +6333,10 @@ socket_gethostbyaddr(PyObject *self, PyObject *args)
63346333
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
63356334
h = gethostbyaddr(ap, al, af);
63366335
_Py_COMP_DIAG_POP
6336+
h_error = h_errno;
63376337
#endif /* HAVE_GETHOSTBYNAME_R */
63386338
Py_END_ALLOW_THREADS
6339-
ret = gethost_common(state, h, SAS2SA(&addr), sizeof(addr), af);
6339+
ret = gethost_common(state, h, SAS2SA(&addr), sizeof(addr), af, h_error);
63406340
#ifdef USE_GETHOSTBYNAME_LOCK
63416341
PyThread_release_lock(netdb_lock);
63426342
#endif

0 commit comments

Comments
 (0)