@@ -43,6 +43,15 @@ _getbytevalue(PyObject* arg, int *value)
4343 return 1 ;
4444}
4545
46+ static inline void
47+ bytearray_write_trailing_null_byte (PyByteArrayObject * self )
48+ {
49+ char * data = PyByteArray_AS_STRING (self );
50+ Py_ssize_t size = PyByteArray_GET_SIZE (self );
51+ data [size ] = '\0' ;
52+ }
53+
54+
4655static void
4756bytearray_reinit_from_bytes (PyByteArrayObject * self , Py_ssize_t size )
4857{
@@ -237,7 +246,7 @@ bytearray_realign_data_lock_held(PyByteArrayObject *self, Py_ssize_t new_size, P
237246 }
238247
239248 if (_PyBytes_ResizeKeepOnError (& self -> ob_bytes_object , alloc ) < 0 ) {
240- self -> ob_start [ size ] = '\0' ; /* Trailing null */
249+ bytearray_write_trailing_null_byte ( self );
241250 return -1 ;
242251 }
243252 }
@@ -306,7 +315,7 @@ bytearray_resize_lock_held(PyObject *self, Py_ssize_t requested_size)
306315 /* Minor downsize; quick exit */
307316 Py_SET_SIZE (self , size );
308317 /* Add mid-buffer null; end provided by bytes. */
309- PyByteArray_AS_STRING ( self )[ size ] = '\0' ; /* Trailing null */
318+ bytearray_write_trailing_null_byte ( _PyByteArray_CAST ( self ));
310319 return 0 ;
311320 }
312321 }
@@ -334,7 +343,7 @@ bytearray_resize_lock_held(PyObject *self, Py_ssize_t requested_size)
334343 bytearray_reinit_from_bytes (obj , size );
335344 if (alloc != size ) {
336345 /* Add mid-buffer null; end provided by bytes. */
337- obj -> ob_bytes [ size ] = '\0' ;
346+ bytearray_write_trailing_null_byte ( obj ) ;
338347 }
339348 return 0 ;
340349}
@@ -1154,7 +1163,7 @@ bytearray___init___impl(PyByteArrayObject *self, PyObject *arg,
11541163 /* Append the byte */
11551164 if (Py_SIZE (self ) + 1 < self -> ob_alloc ) {
11561165 Py_SET_SIZE (self , Py_SIZE (self ) + 1 );
1157- PyByteArray_AS_STRING (self )[ Py_SIZE ( self )] = '\0' ;
1166+ bytearray_write_trailing_null_byte (self );
11581167 }
11591168 else if (PyByteArray_Resize ((PyObject * )self , Py_SIZE (self )+ 1 ) < 0 )
11601169 goto error ;
0 commit comments