@@ -6746,6 +6746,7 @@ socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs)
67466746 PyObject * pobj = (PyObject * )NULL ;
67476747 PyObject * pstr = NULL ;
67486748 const char * hptr , * pptr ;
6749+ Py_ssize_t hlen = 0 , plen = 0 ;
67496750 int family , socktype , protocol , flags ;
67506751 int error ;
67516752 PyObject * all = (PyObject * )NULL ;
@@ -6766,33 +6767,46 @@ socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs)
67666767 return NULL ;
67676768 assert (PyBytes_Check (idna ));
67686769 hptr = PyBytes_AS_STRING (idna );
6770+ hlen = PyBytes_GET_SIZE (idna );
67696771 } else if (PyBytes_Check (hobj )) {
6770- hptr = PyBytes_AsString (hobj );
6772+ hptr = PyBytes_AS_STRING (hobj );
6773+ hlen = PyBytes_GET_SIZE (hobj );
67716774 } else {
67726775 PyErr_SetString (PyExc_TypeError ,
67736776 "getaddrinfo() argument 1 must be string or None" );
67746777 return NULL ;
67756778 }
6779+ if (hptr != NULL && strlen (hptr ) != (size_t )hlen ) {
6780+ PyErr_SetString (PyExc_ValueError ,
6781+ "getaddrinfo() host must not contain null characters" );
6782+ goto err ;
6783+ }
67766784 if (PyLong_CheckExact (pobj )) {
67776785 pstr = PyObject_Str (pobj );
67786786 if (pstr == NULL )
67796787 goto err ;
67806788 assert (PyUnicode_Check (pstr ));
6781- pptr = PyUnicode_AsUTF8 (pstr );
6789+ pptr = PyUnicode_AsUTF8AndSize (pstr , & plen );
67826790 if (pptr == NULL )
67836791 goto err ;
67846792 } else if (PyUnicode_Check (pobj )) {
6785- pptr = PyUnicode_AsUTF8 (pobj );
6793+ pptr = PyUnicode_AsUTF8AndSize (pobj , & plen );
67866794 if (pptr == NULL )
67876795 goto err ;
67886796 } else if (PyBytes_Check (pobj )) {
67896797 pptr = PyBytes_AS_STRING (pobj );
6798+ plen = PyBytes_GET_SIZE (pobj );
67906799 } else if (pobj == Py_None ) {
67916800 pptr = (char * )NULL ;
67926801 } else {
67936802 PyErr_SetString (PyExc_OSError , "Int or String expected" );
67946803 goto err ;
67956804 }
6805+ if (pptr != NULL && strlen (pptr ) != (size_t )plen ) {
6806+ PyErr_SetString (PyExc_ValueError ,
6807+ "getaddrinfo() port must not contain null characters" );
6808+ goto err ;
6809+ }
67966810#if defined(__APPLE__ ) && defined(AI_NUMERICSERV )
67976811 if ((flags & AI_NUMERICSERV ) && (pptr == NULL || (pptr [0 ] == '0' && pptr [1 ] == 0 ))) {
67986812 /* On OSX up to at least OSX 10.8 getaddrinfo crashes
0 commit comments