Title: segfault in mmap object when using __index__ method that closes the mmap · Issue #103987 · python/cpython · GitHub
Open Graph Title: segfault in mmap object when using __index__ method that closes the mmap · Issue #103987 · python/cpython
X Title: segfault in mmap object when using __index__ method that closes the mmap · Issue #103987 · python/cpython
Description: The following (artificial) code segfaults CPython (I tried a bunch of versions, including git main) on my x86 Ubuntu Linux 22.10: import mmap with open("abcds", "w+") as f: f.write("foobar") f.flush() class X(object): def __index__(self)...
Open Graph Description: The following (artificial) code segfaults CPython (I tried a bunch of versions, including git main) on my x86 Ubuntu Linux 22.10: import mmap with open("abcds", "w+") as f: f.write("foobar") f.flus...
X Description: The following (artificial) code segfaults CPython (I tried a bunch of versions, including git main) on my x86 Ubuntu Linux 22.10: import mmap with open("abcds", "w+") as f: f.wr...
Opengraph URL: https://github.com/python/cpython/issues/103987
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"segfault in mmap object when using __index__ method that closes the mmap","articleBody":"The following (artificial) code segfaults CPython (I tried a bunch of versions, including git main) on my x86 Ubuntu Linux 22.10:\r\n\r\n```python\r\nimport mmap\r\n\r\nwith open(\"abcds\", \"w+\") as f:\r\n f.write(\"foobar\")\r\n f.flush()\r\n\r\n class X(object):\r\n def __index__(self):\r\n m.close()\r\n return 1\r\n\r\n m = mmap.mmap(f.fileno(), 6, access=mmap.ACCESS_READ)\r\n\r\n print(m[1])\r\n print(m[X()])\r\n```\r\n\r\nThe problem is this code in `mmapmodule.c`\r\n\r\n```c\r\nstatic PyObject *\r\nmmap_subscript(mmap_object *self, PyObject *item)\r\n{\r\n CHECK_VALID(NULL);\r\n if (PyIndex_Check(item)) {\r\n Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);\r\n if (i == -1 \u0026\u0026 PyErr_Occurred())\r\n return NULL;\r\n if (i \u003c 0)\r\n i += self-\u003esize;\r\n if (i \u003c 0 || i \u003e= self-\u003esize) {\r\n PyErr_SetString(PyExc_IndexError,\r\n \"mmap index out of range\");\r\n return NULL;\r\n }\r\n return PyLong_FromLong(Py_CHARMASK(self-\u003edata[i]));\r\n...\r\n```\r\n\r\nthe `CHECK_VALID(NULL)` call which checks whether the mmap object is closed happens before the `PyNumber_AsSsize_t` call which closes the object (and similarly for the slice handling which happens further down).\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-103990\n* gh-104677\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/cfbolz","@type":"Person","name":"cfbolz"},"datePublished":"2023-04-29T10:20:08.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":9},"url":"https://github.com/103987/cpython/issues/103987"}
| 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:87c57b2e-d9e1-8348-f43d-25dc29260fa3 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8D82:196750:FE1581:15C7400:6969F5F3 |
| html-safe-nonce | e534ee6ee205f46e1d6466c8d9a2b455f9197352a27aa2322e465508ed4b0316 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RDgyOjE5Njc1MDpGRTE1ODE6MTVDNzQwMDo2OTY5RjVGMyIsInZpc2l0b3JfaWQiOiI2NjQ3NDM0ODY5OTI0ODIwNDY3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 65eba15344ca076ceb0757f7518d84c61af97feae4fd78dd0994e673d5df5184 |
| hovercard-subject-tag | issue:1689509261 |
| 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/103987/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6ba70096e8153b284164ef150c395ad79aba99020432fba6533c1eea247b9dac/python/cpython/issues/103987 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6ba70096e8153b284164ef150c395ad79aba99020432fba6533c1eea247b9dac/python/cpython/issues/103987 |
| og:image:alt | The following (artificial) code segfaults CPython (I tried a bunch of versions, including git main) on my x86 Ubuntu Linux 22.10: import mmap with open("abcds", "w+") as f: f.write("foobar") f.flus... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | cfbolz |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7b32f1c7c4549428ee399213e8345494fc55b5637195d3fc5f493657579235e8 |
| 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 | bdde15ad1b403e23b08bbd89b53fbe6bdf688cad |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width