@@ -1889,6 +1889,41 @@ def __index__(self):
1889
1889
with self .assertRaises (IndexError ):
1890
1890
self ._testlimitedcapi .sequence_setitem (b , 0 , Boom ())
1891
1891
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
+
1892
1927
1893
1928
class AssortedBytesTest (unittest .TestCase ):
1894
1929
#
0 commit comments