@@ -6922,6 +6922,7 @@ socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs)
69226922 PyObject * pobj = (PyObject * )NULL ;
69236923 PyObject * pstr = NULL ;
69246924 const char * hptr , * pptr ;
6925+ Py_ssize_t hlen = 0 , plen = 0 ;
69256926 int family , socktype , protocol , flags ;
69266927 int error ;
69276928 PyObject * all = (PyObject * )NULL ;
@@ -6942,13 +6943,20 @@ socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs)
69426943 return NULL ;
69436944 assert (PyBytes_Check (idna ));
69446945 hptr = PyBytes_AS_STRING (idna );
6946+ hlen = PyBytes_GET_SIZE (idna );
69456947 } else if (PyBytes_Check (hobj )) {
6946- hptr = PyBytes_AsString (hobj );
6948+ hptr = PyBytes_AS_STRING (hobj );
6949+ hlen = PyBytes_GET_SIZE (hobj );
69476950 } else {
69486951 PyErr_SetString (PyExc_TypeError ,
69496952 "getaddrinfo() argument 1 must be string or None" );
69506953 return NULL ;
69516954 }
6955+ if (hptr != NULL && strlen (hptr ) != (size_t )hlen ) {
6956+ PyErr_SetString (PyExc_ValueError ,
6957+ "getaddrinfo() host must not contain null characters" );
6958+ goto err ;
6959+ }
69526960 if (PyIndex_Check (pobj )) {
69536961 pstr = PyNumber_Index (pobj );
69546962 if (pstr == NULL )
@@ -6958,21 +6966,27 @@ socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs)
69586966 if (pstr == NULL )
69596967 goto err ;
69606968 assert (PyUnicode_Check (pstr ));
6961- pptr = PyUnicode_AsUTF8 (pstr );
6969+ pptr = PyUnicode_AsUTF8AndSize (pstr , & plen );
69626970 if (pptr == NULL )
69636971 goto err ;
69646972 } else if (PyUnicode_Check (pobj )) {
6965- pptr = PyUnicode_AsUTF8 (pobj );
6973+ pptr = PyUnicode_AsUTF8AndSize (pobj , & plen );
69666974 if (pptr == NULL )
69676975 goto err ;
69686976 } else if (PyBytes_Check (pobj )) {
69696977 pptr = PyBytes_AS_STRING (pobj );
6978+ plen = PyBytes_GET_SIZE (pobj );
69706979 } else if (pobj == Py_None ) {
69716980 pptr = (char * )NULL ;
69726981 } else {
69736982 PyErr_SetString (PyExc_OSError , "Int or String expected" );
69746983 goto err ;
69756984 }
6985+ if (pptr != NULL && strlen (pptr ) != (size_t )plen ) {
6986+ PyErr_SetString (PyExc_ValueError ,
6987+ "getaddrinfo() port must not contain null characters" );
6988+ goto err ;
6989+ }
69766990#if defined(__APPLE__ ) && defined(AI_NUMERICSERV )
69776991 if ((flags & AI_NUMERICSERV ) && (pptr == NULL || (pptr [0 ] == '0' && pptr [1 ] == 0 ))) {
69786992 /* On OSX up to at least OSX 10.8 getaddrinfo crashes
0 commit comments