Skip to content

Commit 7c1fa06

Browse files
committed
fix error handling in socket.gethostbyaddr and socket.gethostbyname_ex functions
1 parent c7b9a13 commit 7c1fa06

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

Modules/socketmodule.c

Lines changed: 14 additions & 4 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;
@@ -6220,12 +6221,15 @@ socket_gethostbyname_ex(PyObject *self, PyObject *args)
62206221
#if defined(HAVE_GETHOSTBYNAME_R_6_ARG)
62216222
gethostbyname_r(name, &hp_allocated, buf, buf_len,
62226223
&h, &errnop);
6224+
h_error = errnop;
62236225
#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
62246226
h = gethostbyname_r(name, &hp_allocated, buf, buf_len, &errnop);
6227+
h_error = errnop;
62256228
#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
62266229
memset((void *) &data, '\0', sizeof(data));
62276230
result = gethostbyname_r(name, &hp_allocated, &data);
62286231
h = (result != 0) ? NULL : &hp_allocated;
6232+
h_error = h_errno;
62296233
#endif
62306234
#else /* not HAVE_GETHOSTBYNAME_R */
62316235
#ifdef USE_GETHOSTBYNAME_LOCK
@@ -6235,6 +6239,7 @@ socket_gethostbyname_ex(PyObject *self, PyObject *args)
62356239
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
62366240
h = gethostbyname(name);
62376241
_Py_COMP_DIAG_POP
6242+
h_error = h_errno;
62386243
#endif /* HAVE_GETHOSTBYNAME_R */
62396244
Py_END_ALLOW_THREADS
62406245
/* Some C libraries would require addr.__ss_family instead of
@@ -6243,7 +6248,7 @@ socket_gethostbyname_ex(PyObject *self, PyObject *args)
62436248
access sa_family. */
62446249
sa = SAS2SA(&addr);
62456250
ret = gethost_common(state, h, SAS2SA(&addr), sizeof(addr),
6246-
sa->sa_family);
6251+
sa->sa_family, h_error);
62476252
#ifdef USE_GETHOSTBYNAME_LOCK
62486253
PyMutex_Unlock(&netdb_lock);
62496254
#endif
@@ -6291,6 +6296,7 @@ socket_gethostbyaddr(PyObject *self, PyObject *args)
62916296
const char *ap;
62926297
int al;
62936298
int af;
6299+
int h_error;
62946300

62956301
if (!PyArg_ParseTuple(args, "et:gethostbyaddr", "idna", &ip_num))
62966302
return NULL;
@@ -6326,13 +6332,16 @@ socket_gethostbyaddr(PyObject *self, PyObject *args)
63266332
gethostbyaddr_r(ap, al, af,
63276333
&hp_allocated, buf, buf_len,
63286334
&h, &errnop);
6335+
h_error = errnop;
63296336
#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
63306337
h = gethostbyaddr_r(ap, al, af,
63316338
&hp_allocated, buf, buf_len, &errnop);
6339+
h_error = errnop;
63326340
#else /* HAVE_GETHOSTBYNAME_R_3_ARG */
63336341
memset((void *) &data, '\0', sizeof(data));
63346342
result = gethostbyaddr_r(ap, al, af, &hp_allocated, &data);
63356343
h = (result != 0) ? NULL : &hp_allocated;
6344+
h_error = h_errno;
63366345
#endif
63376346
#else /* not HAVE_GETHOSTBYNAME_R */
63386347
#ifdef USE_GETHOSTBYNAME_LOCK
@@ -6342,9 +6351,10 @@ socket_gethostbyaddr(PyObject *self, PyObject *args)
63426351
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
63436352
h = gethostbyaddr(ap, al, af);
63446353
_Py_COMP_DIAG_POP
6354+
h_error = h_errno;
63456355
#endif /* HAVE_GETHOSTBYNAME_R */
63466356
Py_END_ALLOW_THREADS
6347-
ret = gethost_common(state, h, SAS2SA(&addr), sizeof(addr), af);
6357+
ret = gethost_common(state, h, SAS2SA(&addr), sizeof(addr), af, h_error);
63486358
#ifdef USE_GETHOSTBYNAME_LOCK
63496359
PyMutex_Unlock(&netdb_lock);
63506360
#endif

0 commit comments

Comments
 (0)