@@ -6557,8 +6557,9 @@ socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs)
65576557 struct addrinfo * res0 = NULL ;
65586558 PyObject * hobj = NULL ;
65596559 PyObject * pobj = (PyObject * )NULL ;
6560- char pbuf [ 30 ] ;
6560+ PyObject * pstr = NULL ;
65616561 const char * hptr , * pptr ;
6562+ Py_ssize_t hlen = 0 , plen = 0 ;
65626563 int family , socktype , protocol , flags ;
65636564 int error ;
65646565 PyObject * all = (PyObject * )NULL ;
@@ -6579,31 +6580,46 @@ socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs)
65796580 return NULL ;
65806581 assert (PyBytes_Check (idna ));
65816582 hptr = PyBytes_AS_STRING (idna );
6583+ hlen = PyBytes_GET_SIZE (idna );
65826584 } else if (PyBytes_Check (hobj )) {
6583- hptr = PyBytes_AsString (hobj );
6585+ hptr = PyBytes_AS_STRING (hobj );
6586+ hlen = PyBytes_GET_SIZE (hobj );
65846587 } else {
65856588 PyErr_SetString (PyExc_TypeError ,
65866589 "getaddrinfo() argument 1 must be string or None" );
65876590 return NULL ;
65886591 }
6592+ if (hptr != NULL && strlen (hptr ) != (size_t )hlen ) {
6593+ PyErr_SetString (PyExc_ValueError ,
6594+ "getaddrinfo() host must not contain null characters" );
6595+ goto err ;
6596+ }
65896597 if (PyLong_CheckExact (pobj )) {
6590- long value = PyLong_AsLong (pobj );
6591- if (value == -1 && PyErr_Occurred ())
6598+ pstr = PyObject_Str (pobj );
6599+ if (pstr == NULL )
6600+ goto err ;
6601+ assert (PyUnicode_Check (pstr ));
6602+ pptr = PyUnicode_AsUTF8AndSize (pstr , & plen );
6603+ if (pptr == NULL )
65926604 goto err ;
6593- PyOS_snprintf (pbuf , sizeof (pbuf ), "%ld" , value );
6594- pptr = pbuf ;
65956605 } else if (PyUnicode_Check (pobj )) {
6596- pptr = PyUnicode_AsUTF8 (pobj );
6606+ pptr = PyUnicode_AsUTF8AndSize (pobj , & plen );
65976607 if (pptr == NULL )
65986608 goto err ;
65996609 } else if (PyBytes_Check (pobj )) {
66006610 pptr = PyBytes_AS_STRING (pobj );
6611+ plen = PyBytes_GET_SIZE (pobj );
66016612 } else if (pobj == Py_None ) {
66026613 pptr = (char * )NULL ;
66036614 } else {
66046615 PyErr_SetString (PyExc_OSError , "Int or String expected" );
66056616 goto err ;
66066617 }
6618+ if (pptr != NULL && strlen (pptr ) != (size_t )plen ) {
6619+ PyErr_SetString (PyExc_ValueError ,
6620+ "getaddrinfo() port must not contain null characters" );
6621+ goto err ;
6622+ }
66076623#if defined(__APPLE__ ) && defined(AI_NUMERICSERV )
66086624 if ((flags & AI_NUMERICSERV ) && (pptr == NULL || (pptr [0 ] == '0' && pptr [1 ] == 0 ))) {
66096625 /* On OSX up to at least OSX 10.8 getaddrinfo crashes
0 commit comments