Uh oh!
There was an error while loading. Please reload this page.
gh-91153: Fix bytearray holding a reference to its internal buffer when calling into potentially mutating __index__ methods - #132379
Conversation
…ay allocation during item assignment
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
I've signed the CLA. Let me know if a NEWS entry is required. I'm not sure it is, especially since there was already a previous change that this is a fix to. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
bast0006
commented
May 17, 2025
Done |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
| self._testlimitedcapi.sequence_setitem(b, 0, Boom()) | ||
| def test_mutating_index_inbounds(self): | ||
| # See gh-91153 |
There was a problem hiding this comment.
Could you add a brief description of the issue?
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
…when `ind.__index__` has side-effects (pythonGH-132379) (cherry picked from commit 5e1e21d) Co-authored-by: Bast <52266665+bast0006@users.noreply.github.com> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Sorry, @bast0006 and @picnixz, I could not cleanly backport this to |
GH-136581 is a backport of this pull request to the 3.14 branch. |
… ...)` when `ind.__index__` has side-effects (pythonGH-132379) (cherry picked from commit 5e1e21d) Co-authored-by: Bast <52266665+bast0006@users.noreply.github.com> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
GH-136582 is a backport of this pull request to the 3.13 branch. |
… when `ind.__index__` has side-effects (GH-132379) (#136581) gh-91153: prevent a crash in `bytearray.__setitem__(ind, ...)` when `ind.__index__` has side-effects (GH-132379) (cherry picked from commit 5e1e21d) Co-authored-by: Bast <52266665+bast0006@users.noreply.github.com> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
…when `ind.__index__` has side-effects (python#132379) Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
…when `ind.__index__` has side-effects (python#132379) Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
…when `ind.__index__` has side-effects (python#132379) Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
…when `ind.__index__` has side-effects (python#132379) Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
… ...)` when `ind.__index__` has side-effects (pythonGH-132379) (python#136581) pythongh-91153: prevent a crash in `bytearray.__setitem__(ind, ...)` when `ind.__index__` has side-effects (pythonGH-132379) (cherry picked from commit 5e1e21d) Co-authored-by: Bast <52266665+bast0006@users.noreply.github.com> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
bytearray's
__setitem__implementation currently grabs a reference to its internal buffer before calling_getbyvalueto determine the index that needs assignment._getbyvaluecan 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
_getbyvalueis 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
_getbyvalueis called to prevent it from being held while any python code is run.__index__with side-effects #91153