@@ -5513,13 +5513,12 @@ _PyUnicode_EncodeUTF32(PyObject *str,
5513
5513
/* four bytes are reserved for each surrogate */
5514
5514
if (moreunits > 1 ) {
5515
5515
Py_ssize_t outpos = out - (uint32_t * ) PyBytes_AS_STRING (v );
5516
- Py_ssize_t morebytes = 4 * (moreunits - 1 );
5517
- if (PyBytes_GET_SIZE (v ) > PY_SSIZE_T_MAX - morebytes ) {
5516
+ if (moreunits >= (PY_SSIZE_T_MAX - PyBytes_GET_SIZE (v )) / 4 ) {
5518
5517
/* integer overflow */
5519
5518
PyErr_NoMemory ();
5520
5519
goto error ;
5521
5520
}
5522
- if (_PyBytes_Resize (& v , PyBytes_GET_SIZE (v ) + morebytes ) < 0 )
5521
+ if (_PyBytes_Resize (& v , PyBytes_GET_SIZE (v ) + 4 * ( moreunits - 1 ) ) < 0 )
5523
5522
goto error ;
5524
5523
out = (uint32_t * ) PyBytes_AS_STRING (v ) + outpos ;
5525
5524
}
@@ -5865,13 +5864,12 @@ _PyUnicode_EncodeUTF16(PyObject *str,
5865
5864
/* two bytes are reserved for each surrogate */
5866
5865
if (moreunits > 1 ) {
5867
5866
Py_ssize_t outpos = out - (unsigned short * ) PyBytes_AS_STRING (v );
5868
- Py_ssize_t morebytes = 2 * (moreunits - 1 );
5869
- if (PyBytes_GET_SIZE (v ) > PY_SSIZE_T_MAX - morebytes ) {
5867
+ if (moreunits >= (PY_SSIZE_T_MAX - PyBytes_GET_SIZE (v )) / 2 ) {
5870
5868
/* integer overflow */
5871
5869
PyErr_NoMemory ();
5872
5870
goto error ;
5873
5871
}
5874
- if (_PyBytes_Resize (& v , PyBytes_GET_SIZE (v ) + morebytes ) < 0 )
5872
+ if (_PyBytes_Resize (& v , PyBytes_GET_SIZE (v ) + 2 * ( moreunits - 1 ) ) < 0 )
5875
5873
goto error ;
5876
5874
out = (unsigned short * ) PyBytes_AS_STRING (v ) + outpos ;
5877
5875
}
@@ -6551,6 +6549,10 @@ _PyUnicode_DecodeUnicodeInternal(const char *s,
6551
6549
1 ))
6552
6550
return NULL ;
6553
6551
6552
+ if (size < 0 ) {
6553
+ PyErr_BadInternalCall ();
6554
+ return NULL ;
6555
+ }
6554
6556
if (size == 0 )
6555
6557
_Py_RETURN_UNICODE_EMPTY ();
6556
6558
@@ -7352,6 +7354,10 @@ decode_code_page_stateful(int code_page,
7352
7354
PyErr_SetString (PyExc_ValueError , "invalid code page number" );
7353
7355
return NULL ;
7354
7356
}
7357
+ if (size < 0 ) {
7358
+ PyErr_BadInternalCall ();
7359
+ return NULL ;
7360
+ }
7355
7361
7356
7362
if (consumed )
7357
7363
* consumed = 0 ;
0 commit comments