gh-156995: Fix `bytearray.take_bytes()` corrupting shared single-byte bytes objects by StanFromIreland · Pull Request #156996 · python/cpython · GitHub
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
@@ -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
Loading