@@ -4519,6 +4519,19 @@ PyDoc_STRVAR(gethostbyname_doc,
45194519Return the IP address (a string of the form '255.255.255.255') for a host." );
45204520
45214521
4522+ static PyObject *
4523+ sock_decode_hostname (const char * name )
4524+ {
4525+ #ifdef MS_WINDOWS
4526+ /* Issue #26227: gethostbyaddr() returns a string encoded
4527+ * to the ANSI code page */
4528+ return PyUnicode_DecodeFSDefault (name );
4529+ #else
4530+ /* Decode from UTF-8 */
4531+ return PyUnicode_FromString (name );
4532+ #endif
4533+ }
4534+
45224535/* Convenience function common to gethostbyname_ex and gethostbyaddr */
45234536
45244537static PyObject *
@@ -4529,6 +4542,7 @@ gethost_common(struct hostent *h, struct sockaddr *addr, size_t alen, int af)
45294542 PyObject * name_list = (PyObject * )NULL ;
45304543 PyObject * addr_list = (PyObject * )NULL ;
45314544 PyObject * tmp ;
4545+ PyObject * name ;
45324546
45334547 if (h == NULL ) {
45344548 /* Let's get real error message to return */
@@ -4637,7 +4651,10 @@ gethost_common(struct hostent *h, struct sockaddr *addr, size_t alen, int af)
46374651 goto err ;
46384652 }
46394653
4640- rtn_tuple = Py_BuildValue ("sOO" , h -> h_name , name_list , addr_list );
4654+ name = sock_decode_hostname (h -> h_name );
4655+ if (name == NULL )
4656+ goto err ;
4657+ rtn_tuple = Py_BuildValue ("NOO" , name , name_list , addr_list );
46414658
46424659 err :
46434660 Py_XDECREF (name_list );
@@ -5619,6 +5636,7 @@ socket_getnameinfo(PyObject *self, PyObject *args)
56195636 struct addrinfo hints , * res = NULL ;
56205637 int error ;
56215638 PyObject * ret = (PyObject * )NULL ;
5639+ PyObject * name ;
56225640
56235641 flags = flowinfo = scope_id = 0 ;
56245642 if (!PyArg_ParseTuple (args , "Oi:getnameinfo" , & sa , & flags ))
@@ -5682,7 +5700,11 @@ socket_getnameinfo(PyObject *self, PyObject *args)
56825700 set_gaierror (error );
56835701 goto fail ;
56845702 }
5685- ret = Py_BuildValue ("ss" , hbuf , pbuf );
5703+
5704+ name = sock_decode_hostname (hbuf );
5705+ if (name == NULL )
5706+ goto fail ;
5707+ ret = Py_BuildValue ("Ns" , name , pbuf );
56865708
56875709fail :
56885710 if (res )
0 commit comments