@@ -6755,6 +6755,7 @@ socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs)
67556755 PyObject * pobj = (PyObject * )NULL ;
67566756 PyObject * pstr = NULL ;
67576757 const char * hptr , * pptr ;
6758+ Py_ssize_t hlen = 0 , plen = 0 ;
67586759 int family , socktype , protocol , flags ;
67596760 int error ;
67606761 PyObject * all = (PyObject * )NULL ;
@@ -6775,33 +6776,46 @@ socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs)
67756776 return NULL ;
67766777 assert (PyBytes_Check (idna ));
67776778 hptr = PyBytes_AS_STRING (idna );
6779+ hlen = PyBytes_GET_SIZE (idna );
67786780 } else if (PyBytes_Check (hobj )) {
6779- hptr = PyBytes_AsString (hobj );
6781+ hptr = PyBytes_AS_STRING (hobj );
6782+ hlen = PyBytes_GET_SIZE (hobj );
67806783 } else {
67816784 PyErr_SetString (PyExc_TypeError ,
67826785 "getaddrinfo() argument 1 must be string or None" );
67836786 return NULL ;
67846787 }
6788+ if (hptr != NULL && strlen (hptr ) != (size_t )hlen ) {
6789+ PyErr_SetString (PyExc_ValueError ,
6790+ "getaddrinfo() host must not contain null characters" );
6791+ goto err ;
6792+ }
67856793 if (PyLong_CheckExact (pobj )) {
67866794 pstr = PyObject_Str (pobj );
67876795 if (pstr == NULL )
67886796 goto err ;
67896797 assert (PyUnicode_Check (pstr ));
6790- pptr = PyUnicode_AsUTF8 (pstr );
6798+ pptr = PyUnicode_AsUTF8AndSize (pstr , & plen );
67916799 if (pptr == NULL )
67926800 goto err ;
67936801 } else if (PyUnicode_Check (pobj )) {
6794- pptr = PyUnicode_AsUTF8 (pobj );
6802+ pptr = PyUnicode_AsUTF8AndSize (pobj , & plen );
67956803 if (pptr == NULL )
67966804 goto err ;
67976805 } else if (PyBytes_Check (pobj )) {
67986806 pptr = PyBytes_AS_STRING (pobj );
6807+ plen = PyBytes_GET_SIZE (pobj );
67996808 } else if (pobj == Py_None ) {
68006809 pptr = (char * )NULL ;
68016810 } else {
68026811 PyErr_SetString (PyExc_OSError , "Int or String expected" );
68036812 goto err ;
68046813 }
6814+ if (pptr != NULL && strlen (pptr ) != (size_t )plen ) {
6815+ PyErr_SetString (PyExc_ValueError ,
6816+ "getaddrinfo() port must not contain null characters" );
6817+ goto err ;
6818+ }
68056819#if defined(__APPLE__ ) && defined(AI_NUMERICSERV )
68066820 if ((flags & AI_NUMERICSERV ) && (pptr == NULL || (pptr [0 ] == '0' && pptr [1 ] == 0 ))) {
68076821 /* On OSX up to at least OSX 10.8 getaddrinfo crashes
0 commit comments