Title: BINARY_OP_SUBSCR_LIST_INT / STORE_SUBSCR_LIST_INT silently depend on _GUARD_TOS_INT enforcing compactness · Issue #150587 · python/cpython · GitHub
Open Graph Title: BINARY_OP_SUBSCR_LIST_INT / STORE_SUBSCR_LIST_INT silently depend on _GUARD_TOS_INT enforcing compactness · Issue #150587 · python/cpython
X Title: BINARY_OP_SUBSCR_LIST_INT / STORE_SUBSCR_LIST_INT silently depend on _GUARD_TOS_INT enforcing compactness · Issue #150587 · python/cpython
Description: Summary _BINARY_OP_SUBSCR_LIST_INT and _STORE_SUBSCR_LIST_INT call _PyLong_CompactValue on the subscript without verifying compactness themselves. They rely entirely on _GUARD_TOS_INT to have already rejected any non-compact int. This is...
Open Graph Description: Summary _BINARY_OP_SUBSCR_LIST_INT and _STORE_SUBSCR_LIST_INT call _PyLong_CompactValue on the subscript without verifying compactness themselves. They rely entirely on _GUARD_TOS_INT to have alrea...
X Description: Summary _BINARY_OP_SUBSCR_LIST_INT and _STORE_SUBSCR_LIST_INT call _PyLong_CompactValue on the subscript without verifying compactness themselves. They rely entirely on _GUARD_TOS_INT to have alrea...
Opengraph URL: https://github.com/python/cpython/issues/150587
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"BINARY_OP_SUBSCR_LIST_INT / STORE_SUBSCR_LIST_INT silently depend on _GUARD_TOS_INT enforcing compactness","articleBody":"## Summary\n\n`_BINARY_OP_SUBSCR_LIST_INT` and `_STORE_SUBSCR_LIST_INT` call `_PyLong_CompactValue`\non the subscript without verifying compactness themselves. They rely entirely on\n`_GUARD_TOS_INT` to have already rejected any non-compact int. This is an undocumented\ninvariant: if `_GUARD_TOS_INT` is ever relaxed, `_PyLong_CompactValue` will be called\non a wide int, reading `ob_digit[0]` as the index instead of the full value and\nsilently returning the wrong list element.\n\n## Details\n\nThe specializer gates `BINARY_OP_SUBSCR_LIST_INT`\n([specialize.c:2391](https://github.com/python/cpython/blob/main/Python/specialize.c#L2391))\nand `STORE_SUBSCR_LIST_INT`\n([specialize.c:1575](https://github.com/python/cpython/blob/main/Python/specialize.c#L1575))\non `_PyLong_IsNonNegativeCompact` — only compact non-negative subscripts trigger\nspecialization. The runtime guard `_GUARD_TOS_INT`\n([bytecodes.c:640](https://github.com/python/cpython/blob/main/Python/bytecodes.c#L640))\ncurrently checks `_PyLong_CheckExactAndCompact`, so the invariant holds today.\n\nHowever, the opcode bodies\n([`_BINARY_OP_SUBSCR_LIST_INT`](https://github.com/python/cpython/blob/main/Python/bytecodes.c#L1138),\n[`_STORE_SUBSCR_LIST_INT`](https://github.com/python/cpython/blob/main/Python/bytecodes.c#L1416))\ndo not re-verify compactness before calling `_PyLong_CompactValue`\n([longintrepr.h:130](https://github.com/python/cpython/blob/main/Include/cpython/longintrepr.h#L130)).\nIf `_GUARD_TOS_INT` were relaxed to accept wider ints (e.g. to `PyLong_CheckExact`),\na wide int subscript would pass the guard and reach `_PyLong_CompactValue` in an\ninvalid state. For a wide positive int `n = ob_digit[0] + ob_digit[1] * 2^30 + ...`,\n`_PyLong_CompactValue` reads only `ob_digit[0]`, silently producing the wrong index.\nOn a debug build this fires `assert(PyUnstable_Long_IsCompact(op))` inside\n`_PyLong_CompactValue`.\n\n## Fix\n\nAdd `EXIT_IF(!_PyLong_IsNonNegativeCompact((PyLongObject *)sub))` to\n`_BINARY_OP_SUBSCR_LIST_INT` and `DEOPT_IF(!_PyLong_IsNonNegativeCompact((PyLongObject *)sub))`\nto `_STORE_SUBSCR_LIST_INT`, immediately before the `_PyLong_CompactValue` call.\nEach opcode then enforces its own precondition rather than relying on the guard's\ncurrent strength.\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-150588\n* gh-153004\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/KRRT7","@type":"Person","name":"KRRT7"},"datePublished":"2026-05-29T19:26:42Z","url":"https://github.com/python/cpython/issues/150587"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| 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:29cec4ba-aeb5-bb7b-bf9c-390e5cd996a3 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AFA6:12B5A4:292CEF:38398E:6AECB604 |
| html-safe-nonce | 941492a10b22dfd8b990274afd44a1999c5b0671c3a4af0784c33c0e1612f4a5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBRkE2OjEyQjVBNDoyOTJDRUY6MzgzOThFOjZBRUNCNjA0IiwidmlzaXRvcl9pZCI6Ijg4MDc2Mzk3NjQ0MDQzMTk3NDgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | cca10589e3c1893de43a429196fcf5da471944ef01239efb84b26af5e77e53ed |
| hovercard-subject-tag | issue:4551063387 |
| 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/150587/issue_layout |
| twitter:image | https://opengraph.githubassets.com/239587753ba228759597b95e3e688570feb00ef7d92d25f5f95c9524577c0721/python/cpython/issues/150587 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/239587753ba228759597b95e3e688570feb00ef7d92d25f5f95c9524577c0721/python/cpython/issues/150587 |
| og:image:alt | Summary _BINARY_OP_SUBSCR_LIST_INT and _STORE_SUBSCR_LIST_INT call _PyLong_CompactValue on the subscript without verifying compactness themselves. They rely entirely on _GUARD_TOS_INT to have alrea... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | KRRT7 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 95ae5445dec4cded2bc4f9d97ede92b9116c0bf381a08a5c844b60b5bddcbc97 |
| 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 | db4ef601a7f034ae2a68e17d6eeaeb7ee9470192 |
| ui-target | canary-1 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width