@@ -6935,6 +6935,7 @@ socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs)
69356935 PyObject * pobj = (PyObject * )NULL ;
69366936 PyObject * pstr = NULL ;
69376937 const char * hptr , * pptr ;
6938+ Py_ssize_t hlen = 0 , plen = 0 ;
69386939 int family , socktype , protocol , flags ;
69396940 int error ;
69406941 PyObject * all = (PyObject * )NULL ;
@@ -6955,13 +6956,20 @@ socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs)
69556956 return NULL ;
69566957 assert (PyBytes_Check (idna ));
69576958 hptr = PyBytes_AS_STRING (idna );
6959+ hlen = PyBytes_GET_SIZE (idna );
69586960 } else if (PyBytes_Check (hobj )) {
6959- hptr = PyBytes_AsString (hobj );
6961+ hptr = PyBytes_AS_STRING (hobj );
6962+ hlen = PyBytes_GET_SIZE (hobj );
69606963 } else {
69616964 PyErr_SetString (PyExc_TypeError ,
69626965 "getaddrinfo() argument 1 must be string or None" );
69636966 return NULL ;
69646967 }
6968+ if (hptr != NULL && strlen (hptr ) != (size_t )hlen ) {
6969+ PyErr_SetString (PyExc_ValueError ,
6970+ "getaddrinfo() host must not contain null characters" );
6971+ goto err ;
6972+ }
69656973 if (PyIndex_Check (pobj )) {
69666974 pstr = PyNumber_Index (pobj );
69676975 if (pstr == NULL )
@@ -6971,21 +6979,27 @@ socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs)
69716979 if (pstr == NULL )
69726980 goto err ;
69736981 assert (PyUnicode_Check (pstr ));
6974- pptr = PyUnicode_AsUTF8 (pstr );
6982+ pptr = PyUnicode_AsUTF8AndSize (pstr , & plen );
69756983 if (pptr == NULL )
69766984 goto err ;
69776985 } else if (PyUnicode_Check (pobj )) {
6978- pptr = PyUnicode_AsUTF8 (pobj );
6986+ pptr = PyUnicode_AsUTF8AndSize (pobj , & plen );
69796987 if (pptr == NULL )
69806988 goto err ;
69816989 } else if (PyBytes_Check (pobj )) {
69826990 pptr = PyBytes_AS_STRING (pobj );
6991+ plen = PyBytes_GET_SIZE (pobj );
69836992 } else if (pobj == Py_None ) {
69846993 pptr = (char * )NULL ;
69856994 } else {
69866995 PyErr_SetString (PyExc_OSError , "Int or String expected" );
69876996 goto err ;
69886997 }
6998+ if (pptr != NULL && strlen (pptr ) != (size_t )plen ) {
6999+ PyErr_SetString (PyExc_ValueError ,
7000+ "getaddrinfo() port must not contain null characters" );
7001+ goto err ;
7002+ }
69897003#if defined(__APPLE__ ) && defined(AI_NUMERICSERV )
69907004 if ((flags & AI_NUMERICSERV ) && (pptr == NULL || (pptr [0 ] == '0' && pptr [1 ] == 0 ))) {
69917005 /* On OSX up to at least OSX 10.8 getaddrinfo crashes
0 commit comments