René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:3c9c0989-4d18-6938-5cea-3204f21e31fe
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id964E:1EC9BC:3980021:4956FFC:6B02029A
html-safe-nonce405533c21ed164e773b20b961b53945a4a83b42bfa2c1c93b4892bb12defc58d
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NjRFOjFFQzlCQzozOTgwMDIxOjQ5NTZGRkM6NkIwMjAyOUEiLCJ2aXNpdG9yX2lkIjoiMjUzOTY0Mzc0NzY1NDgyODY5OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac72156b0ab0199f4699fa6117effe7c7813511d579f4dfc2dbcb81c662ae4f9e9
hovercard-subject-tagissue:6163925502
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/161185/issue_layout
twitter:imagehttps://opengraph.githubassets.com/d1b54f5194f25cf5ac2fef43c217fc0953c34e800457e98c9a467636f07095eb/python/cpython/issues/161185
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/d1b54f5194f25cf5ac2fef43c217fc0953c34e800457e98c9a467636f07095eb/python/cpython/issues/161185
og:image:altIssue 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamevstinner
hostnamegithub.com
expected-hostnamegithub.com
None87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456
turbo-cache-controlno-preview
go-importgithub.com/python/cpython git https://github.com/python/cpython.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id81598961
octolytics-dimension-repository_nwopython/cpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id81598961
octolytics-dimension-repository_network_root_nwopython/cpython
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release423dbb54c5e7bfecc838eb387c1435eb3d0e760d
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/cpython/issues/161185#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F161185
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F161185
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/issues/161185
Reloadhttps://github.com/python/cpython/issues/161185
Reloadhttps://github.com/python/cpython/issues/161185
pythonhttps://github.com/python
cpythonhttps://github.com/python/cpython
Codehttps://github.com/python/cpython
Issues5k+ (5k+)https://github.com/python/cpython/issues
Pull requests2.4k (2.4k)https://github.com/python/cpython/pulls
Actionshttps://github.com/python/cpython/actions
Projectshttps://github.com/python/cpython/projects
Security and qualityhttps://github.com/python/cpython/security
Insightshttps://github.com/python/cpython/insights
https://github.com/python/cpython
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/161185
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/161185
[C API] Py_CLEAR(), Py_SETREF() and Py_XSETREF() memcpy() implementation is not safe with strict aliasinghttps://github.com/python/cpython/issues/161185#top
https://github.com/vstinner
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
topic-C-APIhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-C-API%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
https://github.com/vstinner
vstinnerhttps://github.com/vstinner
on Nov 2, 2026https://github.com/python/cpython/issues/161185#issue-6163925502
@ngoldbaumhttps://github.com/ngoldbaum
python/pythoncapi-compat#187 (comment)https://github.com/python/pythoncapi-compat/pull/187#issuecomment-6041138424
gh-161185: Fix strict aliasing issue in Py_SETREF() macros family #161186https://github.com/python/cpython/pull/161186
[3.15] gh-161185: Fix strict aliasing issue in Py_SETREF() macros family (#161186) #161360https://github.com/python/cpython/pull/161360
[3.14] gh-161185: Fix strict aliasing issue in Py_SETREF() macros family (GH-161186) (GH-161360) #161362https://github.com/python/cpython/pull/161362
[3.14] gh-161185: Fix test_cext: replace PyType_FromSlots() with PyType_FromSpec() #161373https://github.com/python/cpython/pull/161373
vstinnerhttps://github.com/vstinner
topic-C-APIhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-C-API%22
on Nov 2, 2026https://github.com/python/cpython/issues/161185#event-34916220313
vstinnerhttps://github.com/vstinner
on Nov 2, 2026https://github.com/python/cpython/issues/161185#event-34916370194
picnixzhttps://github.com/picnixz
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
on Nov 2, 2026https://github.com/python/cpython/issues/161185#event-34919634908
vstinnerhttps://github.com/vstinner
on Nov 4, 2026https://github.com/python/cpython/issues/161185#event-35173365364
gh-161185: Fix strict aliasing issue in Py_SETREF() macros family (#1…https://github.com/python/cpython/commit/f8e65ee35d05cc47e1e02e716c934b8acaa9b5bd
gh-161185https://github.com/python/cpython/issues/161185
#1…https://github.com/python/cpython/pull/161186
f8e65eehttps://github.com/python/cpython/commit/f8e65ee35d05cc47e1e02e716c934b8acaa9b5bd
pythongh-161185: Fix strict aliasing issue in Py_SETREF() macros fami…https://github.com/vstinner/cpython/commit/d26d75912d0c0ad65d8b3ae03dde85d0548978ef
pythongh-161185https://github.com/python/cpython/issues/161185
d26d759https://github.com/vstinner/cpython/commit/d26d75912d0c0ad65d8b3ae03dde85d0548978ef
[3.15] gh-161185: Fix strict aliasing issue in Py_SETREF() macros fam…https://github.com/python/cpython/commit/0dc1a9602605f15ccf805c37ed4347307cd46cc5
gh-161185https://github.com/python/cpython/issues/161185
0dc1a96https://github.com/python/cpython/commit/0dc1a9602605f15ccf805c37ed4347307cd46cc5
[3.14] gh-161185: Fix strict aliasing issue in Py_SETREF() macros fam…https://github.com/python/cpython/commit/adb9185d8ae31684e5cd7a2649c96d1a66f22ea6
gh-161185https://github.com/python/cpython/issues/161185
adb9185https://github.com/python/cpython/commit/adb9185d8ae31684e5cd7a2649c96d1a66f22ea6
vstinnerhttps://github.com/vstinner
completedhttps://github.com/python/cpython/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
on Nov 4, 2026https://github.com/python/cpython/issues/161185#event-35187192464
vstinnerhttps://github.com/vstinner
on Nov 5, 2026https://github.com/python/cpython/issues/161185#event-35189148413
pythongh-161185: Fix test_cext: replace PyType_FromSlots() with PyTyp…https://github.com/vstinner/cpython/commit/3af55474437c16199549e42e0842f9713e80055d
pythongh-161185https://github.com/python/cpython/issues/161185
3af5547https://github.com/vstinner/cpython/commit/3af55474437c16199549e42e0842f9713e80055d
[3.14] gh-161185: Fix test_cext: replace PyType_FromSlots() with PyTy…https://github.com/python/cpython/commit/8cb34fa96a60e7aca0bef6298a6c9b9f71fc1db6
gh-161185https://github.com/python/cpython/issues/161185
8cb34fahttps://github.com/python/cpython/commit/8cb34fa96a60e7aca0bef6298a6c9b9f71fc1db6
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/cpython/issues/161185
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/cpython/issues/161185
vstinnerhttps://github.com/vstinner
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
topic-C-APIhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-C-API%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.