gh-91153: Fix bytearray holding a reference to its internal buffer when calling into potentially mutating __index__ methods #132379
+40
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
bytearray's
__setitem__
implementation currently grabs a reference to its internal buffer before calling_getbyvalue
to determine the index that needs assignment._getbyvalue
can call into arbitrary python code via__index__
dunders, which could alter the internal buffer and leave said reference dangling.A prior fix for this issue ensures that bounds checking occurs after
_getbyvalue
is called. However, python code is capable of resizing the bytearray, resulting in limited but still broken behavior.This patch ensures that the reference to the internal buffer is fetched only after
_getbyvalue
is called to prevent it from being held while any python code is run.__index__
with side-effects #91153