@@ -5209,13 +5209,12 @@ _PyUnicode_EncodeUTF32(PyObject *str,
5209
5209
/* four bytes are reserved for each surrogate */
5210
5210
if (moreunits > 1 ) {
5211
5211
Py_ssize_t outpos = out - (PY_UINT32_T * ) PyBytes_AS_STRING (v );
5212
- Py_ssize_t morebytes = 4 * (moreunits - 1 );
5213
- if (PyBytes_GET_SIZE (v ) > PY_SSIZE_T_MAX - morebytes ) {
5212
+ if (moreunits >= (PY_SSIZE_T_MAX - PyBytes_GET_SIZE (v )) / 4 ) {
5214
5213
/* integer overflow */
5215
5214
PyErr_NoMemory ();
5216
5215
goto error ;
5217
5216
}
5218
- if (_PyBytes_Resize (& v , PyBytes_GET_SIZE (v ) + morebytes ) < 0 )
5217
+ if (_PyBytes_Resize (& v , PyBytes_GET_SIZE (v ) + 4 * ( moreunits - 1 ) ) < 0 )
5219
5218
goto error ;
5220
5219
out = (PY_UINT32_T * ) PyBytes_AS_STRING (v ) + outpos ;
5221
5220
}
@@ -5552,13 +5551,12 @@ _PyUnicode_EncodeUTF16(PyObject *str,
5552
5551
/* two bytes are reserved for each surrogate */
5553
5552
if (moreunits > 1 ) {
5554
5553
Py_ssize_t outpos = out - (unsigned short * ) PyBytes_AS_STRING (v );
5555
- Py_ssize_t morebytes = 2 * (moreunits - 1 );
5556
- if (PyBytes_GET_SIZE (v ) > PY_SSIZE_T_MAX - morebytes ) {
5554
+ if (moreunits >= (PY_SSIZE_T_MAX - PyBytes_GET_SIZE (v )) / 2 ) {
5557
5555
/* integer overflow */
5558
5556
PyErr_NoMemory ();
5559
5557
goto error ;
5560
5558
}
5561
- if (_PyBytes_Resize (& v , PyBytes_GET_SIZE (v ) + morebytes ) < 0 )
5559
+ if (_PyBytes_Resize (& v , PyBytes_GET_SIZE (v ) + 2 * ( moreunits - 1 ) ) < 0 )
5562
5560
goto error ;
5563
5561
out = (unsigned short * ) PyBytes_AS_STRING (v ) + outpos ;
5564
5562
}
@@ -6250,6 +6248,10 @@ _PyUnicode_DecodeUnicodeInternal(const char *s,
6250
6248
1 ))
6251
6249
return NULL ;
6252
6250
6251
+ if (size < 0 ) {
6252
+ PyErr_BadInternalCall ();
6253
+ return NULL ;
6254
+ }
6253
6255
if (size == 0 )
6254
6256
_Py_RETURN_UNICODE_EMPTY ();
6255
6257
@@ -7052,6 +7054,10 @@ decode_code_page_stateful(int code_page,
7052
7054
PyErr_SetString (PyExc_ValueError , "invalid code page number" );
7053
7055
return NULL ;
7054
7056
}
7057
+ if (size < 0 ) {
7058
+ PyErr_BadInternalCall ();
7059
+ return NULL ;
7060
+ }
7055
7061
7056
7062
if (consumed )
7057
7063
* consumed = 0 ;
0 commit comments