You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
bytearray.__init__() leaves stale bytes when it fails on a list or tuple #160179
The list/tuple fast path in bytearray.__init__() resizes the object to len(arg) before converting the items. When an exact int is outside range(256) it raises ValueError without undoing the resize, so an object that outlives the failed call (a re-initialized bytearray, or a subclass catching the error from super().__init__()) keeps the full length, with bytes that were never written.
b=bytearray(b'abc')
try:
b.__init__([1, 2, 300])
exceptValueError:
passprint(len(b), b)
This prints a length of 3; the last byte is leftover heap content (0xcd on a debug build). Expected 0 bytearray(b''), which is what a non-int item ([1, 2, 1.5]) or an iterator (iter([1, 2, 300]), which leaves b'\x01\x02') gives. With [300] * n the whole object is stale data from a recently freed block. The buffer branch has the same shape when PyBuffer_ToContiguous() fails.
Activity
gh-160179: Leave bytearray empty when __init__() fails midway (GH-160180
[3.15] gh-160179: Leave bytearray empty when __init__() fails midway (G…
[3.14] gh-160179: Leave bytearray empty when __init__() fails midway (G…