@@ -319,6 +319,11 @@ read_str_internal(PyObject *data) {
319319 CPyTagged tagged_size = _read_short_int (data , first );
320320 if (tagged_size == CPY_INT_TAG )
321321 return NULL ;
322+ if ((Py_ssize_t )tagged_size < 0 ) {
323+ // Fail fast for invalid/tampered data.
324+ PyErr_SetString (PyExc_ValueError , "invalid str size" );
325+ return NULL ;
326+ }
322327 Py_ssize_t size = tagged_size >> 1 ;
323328 // Read string content.
324329 char * buf = ((BufferObject * )data )-> buf ;
@@ -437,6 +442,11 @@ read_bytes_internal(PyObject *data) {
437442 CPyTagged tagged_size = _read_short_int (data , first );
438443 if (tagged_size == CPY_INT_TAG )
439444 return NULL ;
445+ if ((Py_ssize_t )tagged_size < 0 ) {
446+ // Fail fast for invalid/tampered data.
447+ PyErr_SetString (PyExc_ValueError , "invalid bytes size" );
448+ return NULL ;
449+ }
440450 Py_ssize_t size = tagged_size >> 1 ;
441451 // Read bytes content.
442452 char * buf = ((BufferObject * )data )-> buf ;
@@ -601,6 +611,10 @@ read_int_internal(PyObject *data) {
601611 Py_ssize_t size_and_sign = _read_short_int (data , first );
602612 if (size_and_sign == CPY_INT_TAG )
603613 return CPY_INT_TAG ;
614+ if ((Py_ssize_t )size_and_sign < 0 ) {
615+ PyErr_SetString (PyExc_ValueError , "invalid int data" );
616+ return CPY_INT_TAG ;
617+ }
604618 bool sign = (size_and_sign >> 1 ) & 1 ;
605619 Py_ssize_t size = size_and_sign >> 2 ;
606620
0 commit comments