10000 GH-91153: Fixup comments and NEWS entry · python/cpython@18e6597 · GitHub
[go: up one dir, main page]

Skip to content

Commit 18e6597

Browse files
bast0006picnixz
andauthored
GH-91153: Fixup comments and NEWS entry
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
1 parent d3d1974 commit 18e6597

File tree

3 files changed

+6
-14
lines changed

3 files changed

+6
-14
lines changed

Lib/test/test_bytes.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,21 +1893,14 @@ def test_mutating_index_inbounds(self):
18931893
# See gh-91153
18941894

18951895
class MutatesOnIndex:
1896-
new_ba: bytearray
18971896

18981897
def __init__(self):
18991898
self.ba = bytearray(0x180)
19001899

19011900
def __index__(self):
1902-
# Clear the original bytearray, mutating it during index assignment.
1903-
# If the internal buffers are held over this operation, they become dangling
1904-
# However, this will fail a bounds check as above (as the clear sets bounds to zero)
19051901
self.ba.clear()
1906-
# At this moment, the bytearray potentially has a dangling pointer
1907-
# Create a new bytearray to catch any writes
1908-
self.new_ba = bytearray(0x180)
1909-
# Ensure bounds check passes
1910-
self.ba.extend([0] * 0x180)
1902+
self.new_ba = bytearray(0x180) # to catch out-of-bounds writes
1903+
self.ba.extend([0] * 0x180) # to check bounds checks
19111904
return 0
19121905

19131906
with self.subTest("skip_bounds_safety"):
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
Fix an issue where a :class:`bytearray` item assignment could crash or write
2-
to the wrong bytearray when resized by the new value's
3-
:meth:`~object.__index__` method.
1+
Fix a crash when a :class:`bytearray` is concurrently mutated during item assignment.

Objects/bytearrayobject.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -709,8 +709,9 @@ bytearray_ass_subscript_lock_held(PyObject *op, PyObject *index, PyObject *value
709709
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(op);
710710
PyByteArrayObject *self = _PyByteArray_CAST(op);
711711
Py_ssize_t start, stop, step, slicelen;
712-
// GH-91153: we cannot store a reference to the internal buffer here, as _getbytevalue might call into python code
713-
// that could then invalidate it.
712+
// Do not store a reference to the internal buffer since
713+
// index.__index__() or _getbytevalue() may alter 'self'.
714+
// See https://github.com/python/cpython/issues/91153.
714715

715716
if (_PyIndex_Check(index)) {
716717
Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);

0 commit comments

Comments
 (0)
0