@@ -429,9 +429,9 @@ element_init(PyObject *self, PyObject *args, PyObject *kwds)
429
429
}
430
430
431
431
LOCAL (int )
432
- element_resize (ElementObject * self , int extra )
432
+ element_resize (ElementObject * self , Py_ssize_t extra )
433
433
{
434
- int size ;
434
+ Py_ssize_t size ;
435
435
PyObject * * children ;
436
436
437
437
/* make sure self->children can hold the given number of extra
@@ -453,6 +453,13 @@ element_resize(ElementObject* self, int extra)
453
453
* be safe.
454
454
*/
455
455
size = size ? size : 1 ;
456
+ if ((size_t )size > PY_SSIZE_T_MAX /sizeof (PyObject * ))
457
+ goto nomemory ;
458
+ if (size > INT_MAX ) {
459
+ PyErr_SetString (PyExc_OverflowError ,
460
+ "too many children" );
461
+ return -1 ;
462
+ }
456
463
if (self -> extra -> children != self -> extra -> _children ) {
457
464
/* Coverity CID #182 size_error: Allocating 1 bytes to pointer
458
465
* "children", which needs at least 4 bytes. Although it's a
@@ -889,7 +896,7 @@ element_setstate_from_attributes(ElementObject *self,
889
896
PyObject * tail ,
890
897
PyObject * children )
891
898
{
892
- Py_ssize_t i , nchildren ;
899
+ int i , nchildren ;
893
900
894
901
if (!tag ) {
895
902
PyErr_SetString (PyExc_TypeError , "tag may not be NULL" );
@@ -914,11 +921,18 @@ element_setstate_from_attributes(ElementObject *self,
914
921
915
922
/* Compute 'nchildren'. */
916
923
if (children ) {
924
+ Py_ssize_t size ;
917
925
if (!PyList_Check (children )) {
918
926
PyErr_SetString (PyExc_TypeError , "'_children' is not a list" );
919
927
return NULL ;
920
928
}
921
- nchildren = PyList_Size (children );
929
+ size = PyList_Size (children );
930
+ /* expat limits nchildren to int */
931
+ if (size > INT_MAX ) {
932
+ PyErr_SetString (PyExc_OverflowError , "too many children" );
933
+ return NULL ;
934
+ }
935
+ nchildren = (int )size ;
922
936
}
923
937
else {
924
938
nchildren = 0 ;
@@ -1505,18 +1519,19 @@ element_set(ElementObject* self, PyObject* args)
1505
1519
}
1506
1520
1507
1521
static int
1508
- element_setitem (PyObject * self_ , Py_ssize_t index , PyObject * item )
1522
+ element_setitem (PyObject * self_ , Py_ssize_t index_ , PyObject * item )
1509
1523
{
1510
1524
ElementObject * self = (ElementObject * ) self_ ;
1511
- int i ;
1525
+ int i , index ;
1512
1526
PyObject * old ;
1513
1527
1514
- if (!self -> extra || index < 0 || index >= self -> extra -> length ) {
1528
+ if (!self -> extra || index_ < 0 || index_ >= self -> extra -> length ) {
1515
1529
PyErr_SetString (
1516
1530
PyExc_IndexError ,
1517
1531
"child assignment index out of range" );
1518
1532
return -1 ;
1519
1533
}
1534
+ index = (int )index_ ;
1520
1535
1521
1536
old = self -> extra -> children [index ];
1522
1537
@@ -1617,6 +1632,7 @@ element_ass_subscr(PyObject* self_, PyObject* item, PyObject* value)
1617
1632
& start , & stop , & step , & slicelen ) < 0 ) {
1618
1633
return -1 ;
1619
1634
}
1635
+ assert (slicelen <= self -> extra -> length );
1620
1636
1621
1637
if (value == NULL ) {
1622
1638
/* Delete slice */
@@ -1678,7 +1694,7 @@ element_ass_subscr(PyObject* self_, PyObject* item, PyObject* value)
1678
1694
(self -> extra -> length - cur ) * sizeof (PyObject * ));
1679
1695
}
1680
1696
1681
- self -> extra -> length -= slicelen ;
1697
+ self -> extra -> length -= ( int ) slicelen ;
1682
1698
1683
1699
/* Discard the recycle list with all the deleted sub-elements */
1684
1700
Py_XDECREF (recycle );
@@ -1714,6 +1730,8 @@ element_ass_subscr(PyObject* self_, PyObject* item, PyObject* value)
1714
1730
return -1 ;
1715
1731
}
1716
1732
}
1733
+ assert (newlen - slicelen <= INT_MAX - self -> extra -> length );
1734
+ assert (newlen - slicelen >= - self -> extra -> length );
1717
1735
1718
1736
if (slicelen > 0 ) {
1719
1737
/* to avoid recursive calls to this method (via decref), move
@@ -1747,7 +1765,7 @@ element_ass_subscr(PyObject* self_, PyObject* item, PyObject* value)
1747
1765
self -> extra -> children [cur ] = element ;
1748
1766
}
1749
1767
1750
- self -> extra -> length += newlen - slicelen ;
1768
+ self -> extra -> length += ( int )( newlen - slicelen ) ;
1751
1769
1752
1770
Py_DECREF (seq );
1753
1771
@@ -3528,8 +3546,14 @@ xmlparser_parse_whole(XMLParserObject* self, PyObject* args)
3528
3546
break ;
3529
3547
}
3530
3548
3549
+ if (PyBytes_GET_SIZE (buffer ) > INT_MAX ) {
3550
+ Py_DECREF (buffer );
3551
+ Py_DECREF (reader );
3552
+ PyErr_SetString (PyExc_OverflowError , "size does not fit in an int" );
3553
+ return NULL ;
3554
+ }
3531
3555
res = expat_parse (
3532
- self , PyBytes_AS_STRING (buffer ), PyBytes_GET_SIZE (buffer ), 0
3556
+ self , PyBytes_AS_STRING (buffer ), ( int ) PyBytes_GET_SIZE (buffer ), 0
3533
3557
);
3534
3558
3535
3559
Py_DECREF (buffer );
0 commit comments