Title: _PyObject_CAST breaks code which assumed a static/dynamic casting · Issue #94731 · python/cpython · GitHub
Open Graph Title: _PyObject_CAST breaks code which assumed a static/dynamic casting · Issue #94731 · python/cpython
X Title: _PyObject_CAST breaks code which assumed a static/dynamic casting · Issue #94731 · python/cpython
Description: pywin32 has, since the 90s, used c++ to "inherit" from a PyObject in some cases. In short, it ends up something like: class MyObject : public PyObject { virtual void some_helper(); } With a layout like this, a reinterpret_cast between a ...
Open Graph Description: pywin32 has, since the 90s, used c++ to "inherit" from a PyObject in some cases. In short, it ends up something like: class MyObject : public PyObject { virtual void some_helper(); } With a layout ...
X Description: pywin32 has, since the 90s, used c++ to "inherit" from a PyObject in some cases. In short, it ends up something like: class MyObject : public PyObject { virtual void some_helper(); } With...
Opengraph URL: https://github.com/python/cpython/issues/94731
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"_PyObject_CAST breaks code which assumed a static/dynamic casting","articleBody":"pywin32 has, since the 90s, used c++ to \"inherit\" from a PyObject in some cases. In short, it ends up something like:\r\n\r\n```\r\nclass MyObject : public PyObject {\r\n virtual void some_helper();\r\n}\r\n```\r\n\r\nWith a layout like this, a `reinterpret_cast` between a `MyObject *` and a `PyObject *` will create invalid pointers.\r\n\r\nConsider something like the following:\r\n\r\n```\r\nvoid MyObject::some_helper() {\r\n Py_DECREF(this);\r\n}\r\n```\r\nThis ends up being a macro which uses `_PyObject_CAST` on the param passed to `Py_DECREF`. [In 3.10, this uses a traditional \"c\" cast](https://github.com/python/cpython/blob/30015de7235e5b4033298b85a164fcd8d96046b3/Include/object.h#L112) , but [in 3.11 it turned into a `reinterpret_cast`](https://github.com/python/cpython/blob/f3212b1ec7a70b69f44acd763221463a69beee4f/Include/pyport.h#L67). In the above example, this causes `Py_DECREF` to be called with an invalid `PyObject *` - which typically doesn't crash at the time, but instead corrupts memory causing a difficult to diagnose crash some time later.\r\n\r\nChanging the code to use explicit `static_cast\u003c\u003e`, or the old-school `(PyObject *)` cast makes things work, but it would be error prone as it would be easy to miss places where it's used via non-obvious nested macros. It also shouldn't be necessary to make this kind of change - c++ code which has worked for this long should continue to work.","author":{"url":"https://github.com/mhammond","@type":"Person","name":"mhammond"},"datePublished":"2022-07-11T05:29:45.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":19},"url":"https://github.com/94731/cpython/issues/94731"}
| 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:aa33cbd5-bda8-c745-9280-33bf0a05af57 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D128:1860A8:D3F5C:129393:696A6217 |
| html-safe-nonce | e48812209140a706ee695272a5353dcbb23d372ad79fec1dde3cc73466ceb9b0 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMTI4OjE4NjBBODpEM0Y1QzoxMjkzOTM6Njk2QTYyMTciLCJ2aXNpdG9yX2lkIjoiMTgxNDY4MjI4OTE1NzMzMzUyNyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 58dc4f2d6d575621b1e4405327c22f832ff113dd2d848370169d047a5ef23bab |
| hovercard-subject-tag | issue:1300226815 |
| 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/94731/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2c04ee26a2098901ab8f5c428107d8f3be6173135fe109cf428b4bfc935f791d/python/cpython/issues/94731 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2c04ee26a2098901ab8f5c428107d8f3be6173135fe109cf428b4bfc935f791d/python/cpython/issues/94731 |
| og:image:alt | pywin32 has, since the 90s, used c++ to "inherit" from a PyObject in some cases. In short, it ends up something like: class MyObject : public PyObject { virtual void some_helper(); } With a layout ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mhammond |
| hostname | github.com |
| expected-hostname | github.com |
| None | 9b7735a184970dd9333b2cbe036c8f3c0a9108c64aaa93827c5a64fc70993392 |
| 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 | 87cbd411c2982752221b5751d583a515b23bf5fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width