Title: _pickle.Unpickler crashes after assigning an empty memo dictionary · Issue #162334 · python/cpython · GitHub
Open Graph Title: _pickle.Unpickler crashes after assigning an empty memo dictionary · Issue #162334 · python/cpython
X Title: _pickle.Unpickler crashes after assigning an empty memo dictionary · Issue #162334 · python/cpython
Description: Bug description Assigning an empty dictionary to the C-accelerated _pickle.Unpickler.memo causes a subsequent protocol-4 MEMOIZE opcode to crash the interpreter instead of completing safely or raising a Python exception. This is a determ...
Open Graph Description: Bug description Assigning an empty dictionary to the C-accelerated _pickle.Unpickler.memo causes a subsequent protocol-4 MEMOIZE opcode to crash the interpreter instead of completing safely or rais...
X Description: Bug description Assigning an empty dictionary to the C-accelerated _pickle.Unpickler.memo causes a subsequent protocol-4 MEMOIZE opcode to crash the interpreter instead of completing safely or rais...
Opengraph URL: https://github.com/python/cpython/issues/162334
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"_pickle.Unpickler crashes after assigning an empty memo dictionary","articleBody":"### Bug description\n\nAssigning an empty dictionary to the C-accelerated `_pickle.Unpickler.memo` causes a subsequent protocol-4 `MEMOIZE` opcode to crash the interpreter instead of completing safely or raising a Python exception.\n\nThis is a **deterministic, single-threaded** reproduction. An AddressSanitizer-enabled non-debug CPython build reports a heap-buffer-overflow (8-byte out-of-bounds read) in `_Unpickler_MemoPut()`.\n\nThis report is being opened as a **regular bug report at the request of the CPython security triage team**. It does not claim that deserializing untrusted pickle data is safe, nor does it claim remote exploitability.\n\n### Minimal reproducer\n\n```python\nimport io\nimport _pickle\n\nu = _pickle.Unpickler(io.BytesIO(b\"\\x80\\x04N\\x94.\"))\nu.memo = {}\nu.load()\n```\n\nThe stream contains `PROTO 4`, `NONE`, `MEMOIZE`, and `STOP`.\n\n### Actual behavior\n\n- **CPython 3.16.0a2+dev** (commit `b09a7e92fb0ab958a52ad94e376402cdd3da09b2`), Clang 23.1.1, non-debug, `--without-pymalloc`, `-O1 -g -fno-omit-frame-pointer -fsanitize=address,undefined`: AddressSanitizer reports `heap-buffer-overflow`, `READ of size 8`, at `Modules/_pickle.c:1742` (`_Unpickler_MemoPut`), immediately after a zero-sized allocation represented as a 1-byte region.\n- **CPython 3.16 debug build**: assertion failure in `_Unpickler_ResizeMemoList()` (`Modules/_pickle.c:1661`), SIGABRT.\n- **CPython 3.14.7 release build**: SIGSEGV.\n\nRelevant symbolized frames on the tested 3.16 build:\n\n```text\n_Unpickler_MemoPut Modules/_pickle.c:1742\n_Unpickler_MemoPut Modules/_pickle.c:1664 (resize path)\nload Modules/_pickle.c:6975\n_pickle_Unpickler_load Modules/_pickle.c:7592\n```\n\n### Expected behavior\n\nAssigning an empty memo dictionary should not cause an out-of-bounds native memory access. Subsequent `load()` should either work or raise a controlled Python exception.\n\n### Suspected root cause\n\nAfter `u.memo = {}`, the internal memo has zero capacity (`memo_size == 0`). The first `MEMOIZE` insertion uses index zero. The resize request calculated from `idx * 2` is therefore zero, leaving no element available at index zero. The subsequent `old_item = self->memo[idx]` reads outside the allocated memo array.\n\nA possible fix is to ensure memo capacity is **strictly greater than the insertion index**, including when `idx == 0`, with appropriate overflow checks. This suggestion has not yet been validated as a patch.\n\n### Related issue\n\n- #111066 describes incorrect behavior when assigning a dictionary to `Unpickler.memo`. This report adds a **deterministic native crash and sanitizer-confirmed out-of-bounds read** after assigning an *empty* dictionary.\n- This does **not** require concurrent access and is distinct from free-threading races.\n\n### CPython versions tested on\n\n3.14.7; 3.16.0a2+dev\n\n### Operating systems tested on\n\nLinux x86-64\n\n### Reporter\n\nHamdy Abou El Anein\n\nI can provide the full ASan log, symbolized trace, and test a regression fix if useful.","author":{"url":"https://github.com/hamdyaea","@type":"Person","name":"hamdyaea"},"datePublished":"2026-11-20T18:41:06Z","url":"https://github.com/python/cpython/issues/162334"}
| 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:656633e7-f40c-cffa-5c8a-412b429468e6 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A63A:1DC4CC:1177E12:1746A7D:6B01E93D |
| html-safe-nonce | 9dd5e04fc465639e5433bc7fc7027481a0b6261ece8b05792102d40bcf504737 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNjNBOjFEQzRDQzoxMTc3RTEyOjE3NDZBN0Q6NkIwMUU5M0QiLCJ2aXNpdG9yX2lkIjoiNjk2ODI0MDk2MjQzMDY5MTY0NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 55f6648ed30c2e971ca3fba7938de2e7896e981846a9d20c6bbef283b571c1dd |
| hovercard-subject-tag | issue:6517539011 |
| 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/162334/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2e776e9336e1a7f2d4680c2cae73e539b0b82d9e3c716d1428edc563e08dd62b/python/cpython/issues/162334 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2e776e9336e1a7f2d4680c2cae73e539b0b82d9e3c716d1428edc563e08dd62b/python/cpython/issues/162334 |
| og:image:alt | Bug description Assigning an empty dictionary to the C-accelerated _pickle.Unpickler.memo causes a subsequent protocol-4 MEMOIZE opcode to crash the interpreter instead of completing safely or rais... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | hamdyaea |
| 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