gh-91153: Fix bytearray holding a reference to its internal buffer when calling into potentially mutating __index__ methods#132379
Merged
picnixz merged 9 commits intopython:mainfrom Jul 12, 2025
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 |
Contributor
Author
|
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. |
picnixz
reviewed
May 17, 2025
Contributor
Author
|
Done |
Misc/NEWS.d/next/Core_and_Builtins/2025-05-17-18-22-12.gh-issue-91153.ioA_83.rst
Outdated
Show resolved
Hide resolved
3c1de75 to
d3d1974
Compare
picnixz
reviewed
Jun 2, 2025
Misc/NEWS.d/next/Core_and_Builtins/2025-05-17-20-56-05.gh-issue-91153.afgtG2.rst
Outdated
Show resolved
Hide resolved
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
ZeroIntensity
approved these changes
Jun 3, 2025
Lib/test/test_bytes.py
Outdated
| self._testlimitedcapi.sequence_setitem(b, 0, Boom()) | ||
|
|
||
| def test_mutating_index_inbounds(self): | ||
| # See gh-91153 |
Member
There was a problem hiding this comment.
Could you add a brief description of the issue?
5853b5c to
50b7998
Compare
50b7998 to
4b7ec9c
Compare
miss-islington
pushed a commit
to miss-islington/cpython
that referenced
this pull request
Jul 12, 2025
…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. |
picnixz
added a commit
to picnixz/cpython
that referenced
this pull request
Jul 12, 2025
… ...)` 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. |
picnixz
added a commit
that referenced
this pull request
Jul 12, 2025
… 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>
Pranjal095
pushed a commit
to Pranjal095/cpython
that referenced
this pull request
Jul 12, 2025
…when `ind.__index__` has side-effects (python#132379) Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
picnixz
added a commit
to picnixz/cpython
that referenced
this pull request
Jul 13, 2025
…when `ind.__index__` has side-effects (python#132379) Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
taegyunkim
pushed a commit
to taegyunkim/cpython
that referenced
this pull request
Aug 4, 2025
…when `ind.__index__` has side-effects (python#132379) Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
Agent-Hellboy
pushed a commit
to Agent-Hellboy/cpython
that referenced
this pull request
Aug 19, 2025
…when `ind.__index__` has side-effects (python#132379) Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
kumaraditya303
pushed a commit
to miss-islington/cpython
that referenced
this pull request
Sep 9, 2025
… ...)` 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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_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