Title: Use-after-free in bytearray.take_bytes() with a reentrant __index__ · Issue #153570 · python/cpython · GitHub
Open Graph Title: Use-after-free in bytearray.take_bytes() with a reentrant __index__ · Issue #153570 · python/cpython
X Title: Use-after-free in bytearray.take_bytes() with a reentrant __index__ · Issue #153570 · python/cpython
Description: Bug report Bug description: The C implementation of bytearray.take_bytes() reads the size before calling the argument's __index__(), then uses that stale size for the bounds check and the buffer reads, so an __index__ that resizes the by...
Open Graph Description: Bug report Bug description: The C implementation of bytearray.take_bytes() reads the size before calling the argument's __index__(), then uses that stale size for the bounds check and the buffer re...
X Description: Bug report Bug description: The C implementation of bytearray.take_bytes() reads the size before calling the argument's __index__(), then uses that stale size for the bounds check and the buffe...
Opengraph URL: https://github.com/python/cpython/issues/153570
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Use-after-free in bytearray.take_bytes() with a reentrant __index__","articleBody":"# Bug report\n\n### Bug description:\n\nThe C implementation of `bytearray.take_bytes()` reads the size before calling\nthe argument's `__index__()`, then uses that stale size for the bounds check and\nthe buffer reads, so an `__index__` that resizes the bytearray reads freed\nmemory (a use-after-free; segfaults on a debug build).\n\n b = bytearray(b'A' * (16 * 1024 * 1024))\n class Evil:\n def __index__(self):\n b.clear()\n return 8 * 1024 * 1024\n b.take_bytes(Evil()) # use-after-free\n\n`bytearray.__setitem__` re-reads the size after `__index__` (GH-91153).\n\n\n### CPython versions tested on:\n\n3.15, 3.16\n\n### Operating systems tested on:\n\nmacOS\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-153572\n* gh-154648\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/tonghuaroot","@type":"Person","name":"tonghuaroot"},"datePublished":"2026-07-11T13:57:38Z","url":"https://github.com/python/cpython/issues/153570"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:e4008310-229e-fdc7-3f2b-ec759852f379 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C820:3CBB3E:3F9BE02:5102ABF:6B02661C |
| html-safe-nonce | c5233bb163ecd1c83037e6e35fd3427ad353d761ce595f5026be979ede255235 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDODIwOjNDQkIzRTozRjlCRTAyOjUxMDJBQkY6NkIwMjY2MUMiLCJ2aXNpdG9yX2lkIjoiNzc4NTI1OTY4OTA2NzA0NjQyOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 7c00d2e7660d15b068b171229152e8ac321b51c38a11eabd18db6abf31705de7 |
| hovercard-subject-tag | issue:4862562075 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/153570/issue_layout |
| twitter:image | https://opengraph.githubassets.com/794f23e3a8ad7d96d43bcc7a0e9e190b2e1690124edb60ff8b5d11bab4893e53/python/cpython/issues/153570 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/794f23e3a8ad7d96d43bcc7a0e9e190b2e1690124edb60ff8b5d11bab4893e53/python/cpython/issues/153570 |
| og:image:alt | Bug report Bug description: The C implementation of bytearray.take_bytes() reads the size before calling the argument's __index__(), then uses that stale size for the bounds check and the buffer re... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | tonghuaroot |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width