@@ -5478,13 +5478,12 @@ _PyUnicode_EncodeUTF32(PyObject *str,
5478
5478
/* four bytes are reserved for each surrogate */
5479
5479
if (moreunits > 1 ) {
5480
5480
Py_ssize_t outpos = out - (uint32_t * ) PyBytes_AS_STRING (v );
5481
- Py_ssize_t morebytes = 4 * (moreunits - 1 );
5482
- if (PyBytes_GET_SIZE (v ) > PY_SSIZE_T_MAX - morebytes ) {
5481
+ if (moreunits >= (PY_SSIZE_T_MAX - PyBytes_GET_SIZE (v )) / 4 ) {
5483
5482
/* integer overflow */
5484
5483
PyErr_NoMemory ();
5485
5484
goto error ;
5486
5485
}
5487
- if (_PyBytes_Resize (& v , PyBytes_GET_SIZE (v ) + morebytes ) < 0 )
5486
+ if (_PyBytes_Resize (& v , PyBytes_GET_SIZE (v ) + 4 * ( moreunits - 1 ) ) < 0 )
5488
5487
goto error ;
5489
5488
out = (uint32_t * ) PyBytes_AS_STRING (v ) + outpos ;
5490
5489
}
@@ -5830,13 +5829,12 @@ _PyUnicode_EncodeUTF16(PyObject *str,
5830
5829
/* two bytes are reserved for each surrogate */
5831
5830
if (moreunits > 1 ) {
5832
5831
Py_ssize_t outpos = out - (unsigned short * ) PyBytes_AS_STRING (v );
5833
- Py_ssize_t morebytes = 2 * (moreunits - 1 );
5834
- if (PyBytes_GET_SIZE (v ) > PY_SSIZE_T_MAX - morebytes ) {
5832
+ if (moreunits >= (PY_SSIZE_T_MAX - PyBytes_GET_SIZE (v )) / 2 ) {
5835
5833
/* integer overflow */
5836
5834
PyErr_NoMemory ();
5837
5835
goto error ;
5838
5836
}
5839
- if (_PyBytes_Resize (& v , PyBytes_GET_SIZE (v ) + morebytes ) < 0 )
5837
+ if (_PyBytes_Resize (& v , PyBytes_GET_SIZE (v ) + 2 * ( moreunits - 1 ) ) < 0 )
5840
5838
goto error ;
5841
5839
out = (unsigned short * ) PyBytes_AS_STRING (v ) + outpos ;
5842
5840
}
@@ -6516,6 +6514,10 @@ _PyUnicode_DecodeUnicodeInternal(const char *s,
6516
6514
1 ))
6517
6515
return NULL ;
6518
6516
6517
+ if (size < 0 ) {
6518
+ PyErr_BadInternalCall ();
6519
+ return NULL ;
6520
+ }
6519
6521
if (size == 0 )
6520
6522
_Py_RETURN_UNICODE_EMPTY ();
6521
6523
@@ -7303,6 +7305,10 @@ decode_code_page_stateful(int code_page,
7303
7305
PyErr_SetString (PyExc_ValueError , "invalid code page number" );
7304
7306
return NULL ;
7305
7307
}
7308
+ if (size < 0 ) {
7309
+ PyErr_BadInternalCall ();
7310
+ return NULL ;
7311
+ }
7306
7312
7307
7313
if (consumed )
7308
7314
* consumed = 0 ;
0 commit comments