@@ -44,11 +44,14 @@ _getbytevalue(PyObject* arg, int *value)
4444}
4545
4646static void
47- bytearray_reinit_from_bytes (PyByteArrayObject * self , Py_ssize_t size ,
48- Py_ssize_t alloc )
47+ bytearray_reinit_from_bytes (PyByteArrayObject * self , Py_ssize_t size )
4948{
49+ Py_ssize_t alloc = PyBytes_GET_SIZE (self -> ob_bytes_object );
50+ assert (0 <= size && size <= alloc );
51+
5052 /* Only the empty bytes may be immortal. */
5153 assert ((alloc == 0 ) == _Py_IsImmortal (self -> ob_bytes_object ));
54+
5255 self -> ob_bytes = self -> ob_start = PyBytes_AS_STRING (self -> ob_bytes_object );
5356 Py_SET_SIZE (self , size );
5457 FT_ATOMIC_STORE_SSIZE_RELAXED (self -> ob_alloc , alloc );
@@ -185,7 +188,7 @@ PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size)
185188 Py_DECREF (new );
186189 return NULL ;
187190 }
188- bytearray_reinit_from_bytes (new , size , size );
191+ bytearray_reinit_from_bytes (new , size );
189192 if (bytes != NULL && size > 0 ) {
190193 memcpy (new -> ob_bytes , bytes , size );
191194 }
@@ -246,7 +249,7 @@ bytearray_resize_lock_held(PyObject *self, Py_ssize_t requested_size)
246249 if (requested_size == 0 ) {
247250 Py_SETREF (obj -> ob_bytes_object ,
248251 Py_GetConstant (Py_CONSTANT_EMPTY_BYTES ));
249- bytearray_reinit_from_bytes (obj , 0 , 0 );
252+ bytearray_reinit_from_bytes (obj , 0 );
250253 return 0 ;
251254 }
252255
@@ -300,15 +303,15 @@ bytearray_resize_lock_held(PyObject *self, Py_ssize_t requested_size)
300303 // Using memmove() would be unsafe, since _PyBytes_ResizeKeepOnError()
301304 // failure code path would be unable to restore the bytearray to its
302305 // previous state.
303- PyObject * resized = PyBytes_FromStringAndSize (NULL , requested_size );
306+ PyObject * resized = PyBytes_FromStringAndSize (NULL , alloc );
304307 if (resized == NULL ) {
305308 return -1 ;
306309 }
307310 memcpy (PyBytes_AS_STRING (resized ), obj -> ob_start , requested_size );
308311 Py_SETREF (obj -> ob_bytes_object , resized );
309312 }
310313
311- bytearray_reinit_from_bytes (obj , size , alloc );
314+ bytearray_reinit_from_bytes (obj , size );
312315 if (alloc != size ) {
313316 /* Add mid-buffer null; end provided by bytes. */
314317 obj -> ob_bytes [size ] = '\0' ;
@@ -939,7 +942,7 @@ bytearray_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
939942 }
940943 PyByteArrayObject * self = _PyByteArray_CAST (op );
941944 self -> ob_bytes_object = Py_GetConstant (Py_CONSTANT_EMPTY_BYTES );
942- bytearray_reinit_from_bytes (self , 0 , 0 );
945+ bytearray_reinit_from_bytes (self , 0 );
943946 self -> ob_exports = 0 ;
944947 return op ;
945948}
@@ -1005,9 +1008,9 @@ bytearray___init___impl(PyByteArrayObject *self, PyObject *arg,
10051008 if (_PyObject_IsUniquelyReferenced (encoded )
10061009 && PyBytes_CheckExact (encoded ))
10071010 {
1008- Py_ssize_t size = Py_SIZE (encoded );
1011+ Py_ssize_t size = PyBytes_GET_SIZE (encoded );
10091012 self -> ob_bytes_object = encoded ;
1010- bytearray_reinit_from_bytes (self , size , size );
1013+ bytearray_reinit_from_bytes (self , size );
10111014 return 0 ;
10121015 }
10131016 new = bytearray_iconcat ((PyObject * )self , encoded );
@@ -1672,7 +1675,7 @@ bytearray_take_bytes_impl(PyByteArrayObject *self, PyObject *n)
16721675 // Point the bytearray towards the buffer with the remaining data.
16731676 PyObject * result = self -> ob_bytes_object ;
16741677 self -> ob_bytes_object = remaining ;
1675- bytearray_reinit_from_bytes (self , remaining_length , remaining_length );
1678+ bytearray_reinit_from_bytes (self , remaining_length );
16761679 return result ;
16771680}
16781681
0 commit comments