1111 *--------------------------------------------------------------------
1212 */
1313
14+ #define PY_SSIZE_T_CLEAN
15+
1416#include "Python.h"
1517#include "structmember.h"
1618
@@ -185,8 +187,8 @@ typedef struct {
185187 PyObject * attrib ;
186188
187189 /* child elements */
188- int length ; /* actual number of items */
189- int allocated ; /* allocated items */
190+ Py_ssize_t length ; /* actual number of items */
191+ Py_ssize_t allocated ; /* allocated items */
190192
191193 /* this either points to _children or to a malloced buffer */
192194 PyObject * * children ;
@@ -251,7 +253,7 @@ LOCAL(void)
251253dealloc_extra (ElementObject * self )
252254{
253255 ElementObjectExtra * myextra ;
254- int i ;
256+ Py_ssize_t i ;
255257
256258 if (!self -> extra )
257259 return ;
@@ -429,9 +431,9 @@ element_init(PyObject *self, PyObject *args, PyObject *kwds)
429431}
430432
431433LOCAL (int )
432- element_resize (ElementObject * self , int extra )
434+ element_resize (ElementObject * self , Py_ssize_t extra )
433435{
434- int size ;
436+ Py_ssize_t size ;
435437 PyObject * * children ;
436438
437439 /* make sure self->children can hold the given number of extra
@@ -442,7 +444,7 @@ element_resize(ElementObject* self, int extra)
442444 return -1 ;
443445 }
444446
445- size = self -> extra -> length + extra ;
447+ size = self -> extra -> length + extra ; /* never overflows */
446448
447449 if (size > self -> extra -> allocated ) {
448450 /* use Python 2.4's list growth strategy */
@@ -453,6 +455,8 @@ element_resize(ElementObject* self, int extra)
453455 * be safe.
454456 */
455457 size = size ? size : 1 ;
458+ if ((size_t )size > PY_SSIZE_T_MAX /sizeof (PyObject * ))
459+ goto nomemory ;
456460 if (self -> extra -> children != self -> extra -> _children ) {
457461 /* Coverity CID #182 size_error: Allocating 1 bytes to pointer
458462 * "children", which needs at least 4 bytes. Although it's a
@@ -613,7 +617,7 @@ element_gc_traverse(ElementObject *self, visitproc visit, void *arg)
613617 Py_VISIT (JOIN_OBJ (self -> tail ));
614618
615619 if (self -> extra ) {
616- int i ;
620+ Py_ssize_t i ;
617621 Py_VISIT (self -> extra -> attrib );
618622
619623 for (i = 0 ; i < self -> extra -> length ; ++ i )
@@ -689,7 +693,7 @@ element_clearmethod(ElementObject* self, PyObject* args)
689693static PyObject *
690694element_copy (ElementObject * self , PyObject * args )
691695{
692- int i ;
696+ Py_ssize_t i ;
693697 ElementObject * element ;
694698
695699 if (!PyArg_ParseTuple (args , ":__copy__" ))
@@ -728,7 +732,7 @@ element_copy(ElementObject* self, PyObject* args)
728732static PyObject *
729733element_deepcopy (ElementObject * self , PyObject * args )
730734{
731- int i ;
735+ Py_ssize_t i ;
732736 ElementObject * element ;
733737 PyObject * tag ;
734738 PyObject * attrib ;
@@ -839,7 +843,7 @@ element_sizeof(PyObject* myself, PyObject* args)
839843static PyObject *
840844element_getstate (ElementObject * self )
841845{
842- int i , noattrib ;
846+ Py_ssize_t i , noattrib ;
843847 PyObject * instancedict = NULL , * children ;
844848
845849 /* Build a list of children. */
@@ -1077,7 +1081,7 @@ element_extend(ElementObject* self, PyObject* args)
10771081static PyObject *
10781082element_find (ElementObject * self , PyObject * args , PyObject * kwds )
10791083{
1080- int i ;
1084+ Py_ssize_t i ;
10811085 PyObject * tag ;
10821086 PyObject * namespaces = Py_None ;
10831087 static char * kwlist [] = {"path" , "namespaces" , 0 };
@@ -1112,7 +1116,7 @@ element_find(ElementObject *self, PyObject *args, PyObject *kwds)
11121116static PyObject *
11131117element_findtext (ElementObject * self , PyObject * args , PyObject * kwds )
11141118{
1115- int i ;
1119+ Py_ssize_t i ;
11161120 PyObject * tag ;
11171121 PyObject * default_value = Py_None ;
11181122 PyObject * namespaces = Py_None ;
@@ -1153,7 +1157,7 @@ element_findtext(ElementObject *self, PyObject *args, PyObject *kwds)
11531157static PyObject *
11541158element_findall (ElementObject * self , PyObject * args , PyObject * kwds )
11551159{
1156- int i ;
1160+ Py_ssize_t i ;
11571161 PyObject * out ;
11581162 PyObject * tag ;
11591163 PyObject * namespaces = Py_None ;
@@ -1238,7 +1242,7 @@ element_get(ElementObject* self, PyObject* args, PyObject* kwds)
12381242static PyObject *
12391243element_getchildren (ElementObject * self , PyObject * args )
12401244{
1241- int i ;
1245+ Py_ssize_t i ;
12421246 PyObject * list ;
12431247
12441248 /* FIXME: report as deprecated? */
@@ -1310,11 +1314,9 @@ element_getitem(PyObject* self_, Py_ssize_t index)
13101314static PyObject *
13111315element_insert (ElementObject * self , PyObject * args )
13121316{
1313- int i ;
1314-
1315- int index ;
1317+ Py_ssize_t index , i ;
13161318 PyObject * element ;
1317- if (!PyArg_ParseTuple (args , "iO !:insert" , & index ,
1319+ if (!PyArg_ParseTuple (args , "nO !:insert" , & index ,
13181320 & Element_Type , & element ))
13191321 return NULL ;
13201322
@@ -1402,7 +1404,7 @@ element_makeelement(PyObject* self, PyObject* args, PyObject* kw)
14021404static PyObject *
14031405element_remove (ElementObject * self , PyObject * args )
14041406{
1405- int i ;
1407+ Py_ssize_t i ;
14061408
14071409 PyObject * element ;
14081410 if (!PyArg_ParseTuple (args , "O!:remove" , & Element_Type , & element ))
@@ -1481,7 +1483,7 @@ static int
14811483element_setitem (PyObject * self_ , Py_ssize_t index , PyObject * item )
14821484{
14831485 ElementObject * self = (ElementObject * ) self_ ;
1484- int i ;
1486+ Py_ssize_t i ;
14851487 PyObject * old ;
14861488
14871489 if (!self -> extra || index < 0 || index >= self -> extra -> length ) {
@@ -2819,12 +2821,13 @@ makeuniversal(XMLParserObject* self, const char* string)
28192821 * message string is the default for the given error_code.
28202822*/
28212823static void
2822- expat_set_error (enum XML_Error error_code , int line , int column , char * message )
2824+ expat_set_error (enum XML_Error error_code , Py_ssize_t line , Py_ssize_t column ,
2825+ const char * message )
28232826{
28242827 PyObject * errmsg , * error , * position , * code ;
28252828 elementtreestate * st = ET_STATE_GLOBAL ;
28262829
2827- errmsg = PyUnicode_FromFormat ("%s: line %d , column %d " ,
2830+ errmsg = PyUnicode_FromFormat ("%s: line %zd , column %zd " ,
28282831 message ? message : EXPAT (ErrorString )(error_code ),
28292832 line , column );
28302833 if (errmsg == NULL )
@@ -2848,7 +2851,7 @@ expat_set_error(enum XML_Error error_code, int line, int column, char *message)
28482851 }
28492852 Py_DECREF (code );
28502853
2851- position = Py_BuildValue ("(ii )" , line , column );
2854+ position = Py_BuildValue ("(nn )" , line , column );
28522855 if (!position ) {
28532856 Py_DECREF (error );
28542857 return ;
@@ -3477,8 +3480,14 @@ xmlparser_parse_whole(XMLParserObject* self, PyObject* args)
34773480 break ;
34783481 }
34793482
3483+ if (PyBytes_GET_SIZE (buffer ) > INT_MAX ) {
3484+ Py_DECREF (buffer );
3485+ Py_DECREF (reader );
3486+ PyErr_SetString (PyExc_OverflowError , "size does not fit in an int" );
3487+ return NULL ;
3488+ }
34803489 res = expat_parse (
3481- self , PyBytes_AS_STRING (buffer ), PyBytes_GET_SIZE (buffer ), 0
3490+ self , PyBytes_AS_STRING (buffer ), ( int ) PyBytes_GET_SIZE (buffer ), 0
34823491 );
34833492
34843493 Py_DECREF (buffer );
0 commit comments