Skip to content

Commit e1e8679

Browse files
committed
pythonGH-91153: Add additional cases to test_mutating_index
1 parent 71ae212 commit e1e8679

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Lib/test/test_bytes.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,6 +1889,41 @@ def __index__(self):
18891889
with self.assertRaises(IndexError):
18901890
self._testlimitedcapi.sequence_setitem(b, 0, Boom())
18911891

1892+
class BoomContinued:
1893+
def __index__(self):
1894+
nonlocal new_ba
1895+
# Clear the original bytearray, mutating it during index assignment.
1896+
# If the internal buffers are held over this operation, they become dangling
1897+
# However, this will fail a bounds check as above (as the clear sets bounds to zero)
1898+
ba.clear()
1899+
# At this moment, the bytearray potentially has a dangling pointer
1900+
# Create a new bytearray to catch any writes
1901+
new_ba = bytearray(0x180)
1902+
# Ensure bounds check passes
1903+
ba.extend([0] * 0x180)
1904+
return 0
1905+
1906+
with self.subTest("skip_bounds_safety"):
1907+
new_ba: bytearray
1908+
ba = bytearray(0x180)
1909+
ba[BoomContinued()] = ord("?")
1910+
self.assertEqual(ba[0], ord("?"), "Assigned bytearray unaltered")
1911+
self.assertEqual(new_ba, bytearray(0x180), "Wrong object altered")
1912+
1913+
with self.subTest("skip_bounds_safety_capi"):
1914+
new_ba: bytearray
1915+
ba = bytearray(0x180)
1916+
self._testlimitedcapi.sequence_setitem(ba, BoomContinued(), ord("?"))
1917+
self.assertEqual(ba[0], ord("?"), "Assigned bytearray unaltered")
1918+
self.assertEqual(new_ba, bytearray(0x180), "Wrong object altered")
1919+
1920+
with self.subTest("skip_bounds_safety_slice"):
1921+
new_ba: bytearray
1922+
ba = bytearray(0x180)
1923+
ba[BoomContinued():1] = [ord("?")]
1924+
self.assertEqual(ba[0], ord("?"), "Assigned bytearray unaltered")
1925+
self.assertEqual(new_ba, bytearray(0x180), "Wrong object altered")
1926+
18921927

18931928
class AssortedBytesTest(unittest.TestCase):
18941929
#

0 commit comments

Comments
 (0)