Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Lib/test/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1611,6 +1611,14 @@ def test_take_bytes(self):
self.assertRaises(BufferError, ba.take_bytes)
self.assertEqual(ba.take_bytes(), b'abc')

# Leaving one byte must not adopt the shared single-byte bytes object
# as the buffer.
ba = bytearray(b'abc')
self.assertEqual(ba.take_bytes(2), b'ab')
ba[0] = ord('A')
self.assertEqual(ba, bytearray(b'A'))
self.assertEqual(ord(b'c'), ord('c'))

@support.cpython_only # tests an implementation detail
def test_take_bytes_optimization(self):
# Validate optimization around taking lots of little chunks out of a
Expand Down Expand Up @@ -3055,5 +3063,18 @@ def resize_stress(ba):
with threading_helper.start_threads(threads):
pass

@threading_helper.reap_threads
@threading_helper.requires_working_threading()
def test_free_threading_bytearray_resize_other_thread(self):
# Shrinking a bytearray whose buffer another thread owns must not
# adopt the immortal single-byte bytes object a the buffer.
ba = bytearray(b'abc')
thread = threading.Thread(target=ba.resize, args=(1,))
with threading_helper.start_threads([thread]):
pass
ba[0] = ord('X')
self.assertEqual(ba, bytearray(b'X'))
self.assertEqual(ord(b'a'), ord('a'))

if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Fix :class:`bytearray` sharing its buffer with the single-byte :class:`bytes`
object of the same value, so that writing to the bytearray modified that
:class:`bytes` object. This happened with :meth:`bytearray.take_bytes` when
exactly one byte remained, and on the free-threaded build when a bytearray was
shrunk to one byte from another thread.
13 changes: 9 additions & 4 deletions Objects/bytearrayobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ _getbytevalue(PyObject* arg, int *value)

static void
bytearray_reinit_from_bytes(PyByteArrayObject *self, Py_ssize_t size,
Py_ssize_t alloc) {
Py_ssize_t alloc)
{
/* Only the empty bytes may be immortal. */
assert((alloc == 0) == _Py_IsImmortal(self->ob_bytes_object));
self->ob_bytes = self->ob_start = PyBytes_AS_STRING(self->ob_bytes_object);
Py_SET_SIZE(self, size);
FT_ATOMIC_STORE_SSIZE_RELAXED(self->ob_alloc, alloc);
Expand Down Expand Up @@ -1619,12 +1622,14 @@ bytearray_take_bytes_impl(PyByteArrayObject *self, PyObject *n)
return ret;
}

// Copy remaining bytes to a new bytes.
PyObject *remaining = PyBytes_FromStringAndSize(self->ob_start + to_take,
remaining_length);
// Copy remaining bytes to a new bytes. Allocate and then copy
// so we don't get a shared immortal one-character singleton!
PyObject *remaining = PyBytes_FromStringAndSize(NULL, remaining_length);
if (remaining == NULL) {
return NULL;
}
memcpy(PyBytes_AS_STRING(remaining), self->ob_start + to_take,
remaining_length);

// If the bytes are offset inside the buffer must first align.
if (self->ob_start != self->ob_bytes) {
Expand Down
14 changes: 6 additions & 8 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3368,14 +3368,12 @@ _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize)
return 0;
}
if (!_PyObject_IsUniquelyReferenced(v)) {
if (oldsize < newsize) {
*pv = _PyBytes_FromSize(newsize, 0);
if (*pv) {
memcpy(PyBytes_AS_STRING(*pv), PyBytes_AS_STRING(v), oldsize);
}
}
else {
*pv = PyBytes_FromStringAndSize(PyBytes_AS_STRING(v), newsize);
// Allocate and then copy so we don't get a shared immortal
// one-character singleton!
*pv = _PyBytes_FromSize(newsize, 0);
if (*pv) {
memcpy(PyBytes_AS_STRING(*pv), PyBytes_AS_STRING(v),
Py_MIN(oldsize, newsize));
}
Py_DECREF(v);
return (*pv == NULL) ? -1 : 0;
Expand Down
Loading