Title: `bytearray` reads and writes outside its buffer after `del b[:n]` fails with `MemoryError` · Issue #160303 · python/cpython · GitHub
Open Graph Title: `bytearray` reads and writes outside its buffer after `del b[:n]` fails with `MemoryError` · Issue #160303 · python/cpython
X Title: `bytearray` reads and writes outside its buffer after `del b[:n]` fails with `MemoryError` · Issue #160303 · python/cpython
Description: Bug report Bug description: If the allocation that shrinks the storage fails during del b[:n] (or b[:n] = shorter) on a bytearray, MemoryError is raised and the bytearray keeps its old length but starts n bytes before its buffer. Later r...
Open Graph Description: Bug report Bug description: If the allocation that shrinks the storage fails during del b[:n] (or b[:n] = shorter) on a bytearray, MemoryError is raised and the bytearray keeps its old length but s...
X Description: Bug report Bug description: If the allocation that shrinks the storage fails during del b[:n] (or b[:n] = shorter) on a bytearray, MemoryError is raised and the bytearray keeps its old length but s...
Opengraph URL: https://github.com/python/cpython/issues/160303
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`bytearray` reads and writes outside its buffer after `del b[:n]` fails with `MemoryError`","articleBody":"# Bug report\n\n### Bug description:\n\nIf the allocation that shrinks the storage fails during `del b[:n]` (or `b[:n] = shorter`) on a `bytearray`, `MemoryError` is raised and the bytearray keeps its old length but starts `n` bytes before its buffer. Later reads return memory in front of the buffer and later writes modify it. `bytearray_setslice_linear()` undoes its `ob_start` advance on failure, but since gh-157242 (GH-157340) `bytearray_resize_storage()` has already moved the data and reset `ob_start` before the allocation is attempted.\n\n```python\nimport _testcapi\n\nb = bytearray(b\"A\" * 100 + b\"B\" * 20)\n_testcapi.set_nomemory(0, 1)\ntry:\n del b[:100]\nexcept MemoryError:\n print(\"MemoryError\")\nfinally:\n _testcapi.remove_mem_hooks()\nprint(len(b), b == b\"A\" * 100 + b\"B\" * 20)\nprint(bytes(b)[60:])\n```\n\nExpected `120 True` and the original contents. On a debug build of main this prints\n\n```\nMemoryError\n120 False\nb'o\\xfd\\xfd\\xfd\\xfd\\xfd\\xfd\\xfd\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\xa0\\xdd\\x86\\xc8(^\\x00\\x00x\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\xff\\xff\\xff\\xff\\xff\\xff\\xff\\xffBBBBBBBBBBBBBBBBBBBB'\n```\n\nThe bytes before the `B`s are allocator padding and the header of the internal bytes object. The 3.15 branch has the same `bytearray_resize_storage()` code; 3.14 is not affected.\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nLinux\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-160304\n* gh-161836\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/gpshead","@type":"Person","name":"gpshead"},"datePublished":"2026-10-22T18:49:10Z","url":"https://github.com/python/cpython/issues/160303"}
| 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:e0175efe-5a06-3d85-0860-327680380206 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8116:1087D4:33AC184:4508BB5:6B02023D |
| html-safe-nonce | 6c37dee6ffb75819e6f9e3246f0dea9e74f0e6e40f64f70d5399cb8f21ec013c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MTE2OjEwODdENDozM0FDMTg0OjQ1MDhCQjU6NkIwMjAyM0QiLCJ2aXNpdG9yX2lkIjoiNDUyNDE5NjYzMDE2NTA2MjIwNSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 9789a05ce73ea96b8c2d3c09c8b123be283b2aa3c1367b91893b470040cdd587 |
| hovercard-subject-tag | issue:5985492829 |
| 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/160303/issue_layout |
| twitter:image | https://opengraph.githubassets.com/318dbd8bf3acf3825a54c74d0aeadcfa4efe3f327c5cdc069504d59df0aad7b2/python/cpython/issues/160303 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/318dbd8bf3acf3825a54c74d0aeadcfa4efe3f327c5cdc069504d59df0aad7b2/python/cpython/issues/160303 |
| og:image:alt | Bug report Bug description: If the allocation that shrinks the storage fails during del b[:n] (or b[:n] = shorter) on a bytearray, MemoryError is raised and the bytearray keeps its old length but s... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | gpshead |
| 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