@@ -186,6 +186,11 @@ PyUnicodeObject *_PyUnicode_New(int length)
186186 return unicode_empty ;
187187 }
188188
189+ /* Ensure we won't overflow the size. */
190+ if (length > ((INT_MAX / sizeof (Py_UNICODE )) - 1 )) {
191+ return (PyUnicodeObject * )PyErr_NoMemory ();
192+ }
193+
189194 /* Unicode freelist & memory allocation */
190195 if (unicode_freelist ) {
191196 unicode = unicode_freelist ;
@@ -1040,6 +1045,9 @@ PyObject *PyUnicode_EncodeUTF7(const Py_UNICODE *s,
10401045 char * out ;
10411046 char * start ;
10421047
1048+ if (cbAllocated / 5 != size )
1049+ return PyErr_NoMemory ();
1050+
10431051 if (size == 0 )
10441052 return PyString_FromStringAndSize (NULL , 0 );
10451053
@@ -1638,6 +1646,7 @@ PyUnicode_EncodeUTF16(const Py_UNICODE *s,
16381646{
16391647 PyObject * v ;
16401648 unsigned char * p ;
1649+ int nsize , bytesize ;
16411650#ifdef Py_UNICODE_WIDE
16421651 int i , pairs ;
16431652#else
@@ -1662,8 +1671,15 @@ PyUnicode_EncodeUTF16(const Py_UNICODE *s,
16621671 if (s [i ] >= 0x10000 )
16631672 pairs ++ ;
16641673#endif
1665- v = PyString_FromStringAndSize (NULL ,
1666- 2 * (size + pairs + (byteorder == 0 )));
1674+ /* 2 * (size + pairs + (byteorder == 0)) */
1675+ if (size > INT_MAX ||
1676+ size > INT_MAX - pairs - (byteorder == 0 ))
1677+ return PyErr_NoMemory ();
1678+ nsize = (size + pairs + (byteorder == 0 ));
1679+ bytesize = nsize * 2 ;
1680+ if (bytesize / 2 != nsize )
1681+ return PyErr_NoMemory ();
1682+ v = PyString_FromStringAndSize (NULL , bytesize );
16671683 if (v == NULL )
16681684 return NULL ;
16691685
@@ -1977,6 +1993,11 @@ PyObject *unicodeescape_string(const Py_UNICODE *s,
19771993 char * p ;
19781994
19791995 static const char * hexdigit = "0123456789abcdef" ;
1996+ #ifdef Py_UNICODE_WIDE
1997+ const int expandsize = 10 ;
1998+ #else
1999+ const int expandsize = 6 ;
2000+ #endif
19802001
19812002 /* Initial allocation is based on the longest-possible unichr
19822003 escape.
@@ -1992,13 +2013,12 @@ PyObject *unicodeescape_string(const Py_UNICODE *s,
19922013 escape.
19932014 */
19942015
2016+ if (size > (INT_MAX - 2 - 1 ) / expandsize )
2017+ return PyErr_NoMemory ();
2018+
19952019 repr = PyString_FromStringAndSize (NULL ,
19962020 2
1997- #ifdef Py_UNICODE_WIDE
1998- + 10 * size
1999- #else
2000- + 6 * size
2001- #endif
2021+ + expandsize * size
20022022 + 1 );
20032023 if (repr == NULL )
20042024 return NULL ;
@@ -2239,12 +2259,16 @@ PyObject *PyUnicode_EncodeRawUnicodeEscape(const Py_UNICODE *s,
22392259 char * q ;
22402260
22412261 static const char * hexdigit = "0123456789abcdef" ;
2242-
22432262#ifdef Py_UNICODE_WIDE
2244- repr = PyString_FromStringAndSize ( NULL , 10 * size ) ;
2263+ const int expandsize = 10 ;
22452264#else
2246- repr = PyString_FromStringAndSize ( NULL , 6 * size ) ;
2265+ const int expandsize = 6 ;
22472266#endif
2267+
2268+ if (size > INT_MAX / expandsize )
2269+ return PyErr_NoMemory ();
2270+
2271+ repr = PyString_FromStringAndSize (NULL , expandsize * size );
22482272 if (repr == NULL )
22492273 return NULL ;
22502274 if (size == 0 )
@@ -4289,6 +4313,11 @@ PyUnicodeObject *pad(PyUnicodeObject *self,
42894313 return self ;
42904314 }
42914315
4316+ if (left > INT_MAX - self -> length ||
4317+ right > INT_MAX - (left + self -> length )) {
4318+ PyErr_SetString (PyExc_OverflowError , "padded string is too long" );
4319+ return NULL ;
4320+ }
42924321 u = _PyUnicode_New (left + self -> length + right );
42934322 if (u ) {
42944323 if (left )
0 commit comments