Title: [C API] Py_CLEAR(), Py_SETREF() and Py_XSETREF() memcpy() implementation is not safe with strict aliasing · Issue #161185 · python/cpython · GitHub
Open Graph Title: [C API] Py_CLEAR(), Py_SETREF() and Py_XSETREF() memcpy() implementation is not safe with strict aliasing · Issue #161185 · python/cpython
X Title: [C API] Py_CLEAR(), Py_SETREF() and Py_XSETREF() memcpy() implementation is not safe with strict aliasing · Issue #161185 · python/cpython
Description: Issue reported by @ngoldbaum on pythoncapi-compat: python/pythoncapi-compat#187 (comment). Copy/paste of his message. I asked an AI model to look this over and it spotted an issue (CPython's fallback also has this problem): The memcpy() ...
Open Graph Description: Issue reported by @ngoldbaum on pythoncapi-compat: python/pythoncapi-compat#187 (comment). Copy/paste of his message. I asked an AI model to look this over and it spotted an issue (CPython's fallba...
X Description: Issue reported by @ngoldbaum on pythoncapi-compat: python/pythoncapi-compat#187 (comment). Copy/paste of his message. I asked an AI model to look this over and it spotted an issue (CPython's fa...
Opengraph URL: https://github.com/python/cpython/issues/161185
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[C API] Py_CLEAR(), Py_SETREF() and Py_XSETREF() memcpy() implementation is not safe with strict aliasing","articleBody":"Issue reported by @ngoldbaum on pythoncapi-compat: https://github.com/python/pythoncapi-compat/pull/187#issuecomment-6041138424. Copy/paste of his message.\n\n---\n\nI asked an AI model to look this over and it spotted an issue (CPython's fallback also has this problem):\n\nThe `memcpy()` protects the write, but the preceding read still violates strict aliasing:\n\n```c\nPyObject **_tmp_dst_ptr = _Py_CAST(PyObject**, &(dst));\nPyObject *_tmp_old_dst = (*_tmp_dst_ptr);\n```\n\nIf `dst` is a `PyTypeObject *`, dereferencing `_tmp_dst_ptr` accesses a `PyTypeObject *` object through a `PyObject *` lvalue. GCC can therefore assume that this read does not observe a write made through `PyTypeObject **`.\n\nHere is a standalone reproducer using the exact `Py_XSETREF()` implementation from this PR:\n\n```cpp\n#include \u003cPython.h>\n\n#include \u003ccstdio>\n#include \u003ccstring>\n\n#ifndef _Py_CAST\n# define _Py_CAST(type, expr) ((type)(expr))\n#endif\n#ifndef _PyObject_CAST\n# define _PyObject_CAST(obj) _Py_CAST(PyObject *, (obj))\n#endif\n\n#define REPRO_XSETREF(dst, src) \\\n do { \\\n PyObject **_tmp_dst_ptr = _Py_CAST(PyObject **, &(dst)); \\\n PyObject *_tmp_old_dst = (*_tmp_dst_ptr); \\\n PyObject *_tmp_src = _PyObject_CAST(src); \\\n memcpy(_tmp_dst_ptr, &_tmp_src, sizeof(PyObject *)); \\\n Py_XDECREF(_tmp_old_dst); \\\n } while (0)\n\n__attribute__((noinline))\nstatic void\nreplace(PyTypeObject **a, PyTypeObject **b,\n PyTypeObject *first, PyTypeObject *second)\n{\n *a = first;\n REPRO_XSETREF(*b, second);\n}\n\nint\nmain()\n{\n Py_Initialize();\n\n PyType_Slot slots[] = {{0, NULL}};\n PyType_Spec spec = {\n \"repro.T\", sizeof(PyObject), 0, Py_TPFLAGS_DEFAULT, slots\n };\n PyObject *first = PyType_FromSpec(&spec);\n PyObject *second = PyType_FromSpec(&spec);\n if (first == NULL || second == NULL) {\n PyErr_Print();\n return 1;\n }\n\n Py_ssize_t before = Py_REFCNT(first);\n PyTypeObject *slot = NULL;\n\n // Both pointer arguments alias. The assignment makes first the old value\n // that REPRO_XSETREF() must release before storing second.\n replace(&slot, &slot,\n reinterpret_cast\u003cPyTypeObject *>(Py_NewRef(first)),\n reinterpret_cast\u003cPyTypeObject *>(Py_NewRef(second)));\n\n std::printf(\"first refcount: %zd; expected: %zd\\n\",\n Py_REFCNT(first), before);\n return 0;\n}\n```\n\nOn CPython 3.10 with GCC 15.2.0:\n\n```console\n$ g++ -std=c++11 $(python3.10-config --cflags) \\\n -DPy_LIMITED_API=0x030a0000 -O2 -fstrict-aliasing \\\n repro.cpp $(python3.10-config --embed --ldflags) -o repro\n$ ./repro\nfirst refcount: 3; expected: 2\n```\n\nThe reference acquired by `Py_NewRef(first)` was not released. Compiling the same source with `-fno-strict-aliasing` produces the expected refcount of 2.\n\nWith `Py_SETREF()` instead of `Py_XSETREF()`, the stale value can be `NULL`, causing a segmentation fault when it is passed to `Py_DECREF()`.\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-161186\n* gh-161360\n* gh-161362\n* gh-161373\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/vstinner","@type":"Person","name":"vstinner"},"datePublished":"2026-11-02T13:53:41Z","url":"https://github.com/python/cpython/issues/161185"}
| 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:3c9c0989-4d18-6938-5cea-3204f21e31fe |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 964E:1EC9BC:3980021:4956FFC:6B02029A |
| html-safe-nonce | 405533c21ed164e773b20b961b53945a4a83b42bfa2c1c93b4892bb12defc58d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NjRFOjFFQzlCQzozOTgwMDIxOjQ5NTZGRkM6NkIwMjAyOUEiLCJ2aXNpdG9yX2lkIjoiMjUzOTY0Mzc0NzY1NDgyODY5OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 72156b0ab0199f4699fa6117effe7c7813511d579f4dfc2dbcb81c662ae4f9e9 |
| hovercard-subject-tag | issue:6163925502 |
| 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/161185/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d1b54f5194f25cf5ac2fef43c217fc0953c34e800457e98c9a467636f07095eb/python/cpython/issues/161185 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d1b54f5194f25cf5ac2fef43c217fc0953c34e800457e98c9a467636f07095eb/python/cpython/issues/161185 |
| og:image:alt | Issue reported by @ngoldbaum on pythoncapi-compat: python/pythoncapi-compat#187 (comment). Copy/paste of his message. I asked an AI model to look this over and it spotted an issue (CPython's fallba... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | vstinner |
| 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