@@ -7018,6 +7018,7 @@ _socket_getaddrinfo_impl(PyObject *module, PyObject *hobj, PyObject *pobj,
70187018 struct addrinfo * res0 = NULL ;
70197019 PyObject * pstr = NULL ;
70207020 const char * hptr , * pptr ;
7021+ Py_ssize_t hlen = 0 , plen = 0 ;
70217022 int error ;
70227023 PyObject * all = (PyObject * )NULL ;
70237024 PyObject * idna = NULL ;
@@ -7030,13 +7031,20 @@ _socket_getaddrinfo_impl(PyObject *module, PyObject *hobj, PyObject *pobj,
70307031 return NULL ;
70317032 assert (PyBytes_Check (idna ));
70327033 hptr = PyBytes_AS_STRING (idna );
7034+ hlen = PyBytes_GET_SIZE (idna );
70337035 } else if (PyBytes_Check (hobj )) {
7034- hptr = PyBytes_AsString (hobj );
7036+ hptr = PyBytes_AS_STRING (hobj );
7037+ hlen = PyBytes_GET_SIZE (hobj );
70357038 } else {
70367039 PyErr_SetString (PyExc_TypeError ,
70377040 "getaddrinfo() argument 1 must be string or None" );
70387041 return NULL ;
70397042 }
7043+ if (hptr != NULL && strlen (hptr ) != (size_t )hlen ) {
7044+ PyErr_SetString (PyExc_ValueError ,
7045+ "getaddrinfo() host must not contain null characters" );
7046+ goto err ;
7047+ }
70407048 if (PyIndex_Check (pobj )) {
70417049 pstr = PyNumber_Index (pobj );
70427050 if (pstr == NULL )
@@ -7046,21 +7054,27 @@ _socket_getaddrinfo_impl(PyObject *module, PyObject *hobj, PyObject *pobj,
70467054 if (pstr == NULL )
70477055 goto err ;
70487056 assert (PyUnicode_Check (pstr ));
7049- pptr = PyUnicode_AsUTF8 (pstr );
7057+ pptr = PyUnicode_AsUTF8AndSize (pstr , & plen );
70507058 if (pptr == NULL )
70517059 goto err ;
70527060 } else if (PyUnicode_Check (pobj )) {
7053- pptr = PyUnicode_AsUTF8 (pobj );
7061+ pptr = PyUnicode_AsUTF8AndSize (pobj , & plen );
70547062 if (pptr == NULL )
70557063 goto err ;
70567064 } else if (PyBytes_Check (pobj )) {
70577065 pptr = PyBytes_AS_STRING (pobj );
7066+ plen = PyBytes_GET_SIZE (pobj );
70587067 } else if (pobj == Py_None ) {
70597068 pptr = (char * )NULL ;
70607069 } else {
70617070 PyErr_SetString (PyExc_OSError , "Int or String expected" );
70627071 goto err ;
70637072 }
7073+ if (pptr != NULL && strlen (pptr ) != (size_t )plen ) {
7074+ PyErr_SetString (PyExc_ValueError ,
7075+ "getaddrinfo() port must not contain null characters" );
7076+ goto err ;
7077+ }
70647078#if defined(__APPLE__ ) && defined(AI_NUMERICSERV )
70657079 if ((flags & AI_NUMERICSERV ) && (pptr == NULL || (pptr [0 ] == '0' && pptr [1 ] == 0 ))) {
70667080 /* On OSX up to at least OSX 10.8 getaddrinfo crashes
0 commit comments