Title: Segfault: `Py_DECREF(NULL)` in `setup_context`/`do_warn` (`_warnings.c`) when emitting a warning under MemoryError · Issue #151673 · python/cpython · GitHub
Open Graph Title: Segfault: `Py_DECREF(NULL)` in `setup_context`/`do_warn` (`_warnings.c`) when emitting a warning under MemoryError · Issue #151673 · python/cpython
X Title: Segfault: `Py_DECREF(NULL)` in `setup_context`/`do_warn` (`_warnings.c`) when emitting a warning under MemoryError · Issue #151673 · python/cpython
Description: Crash report What happened? AI Disclaimer: this issue was drafted by Claude Code, which also generated the reduced reproducer. Emitting a warning while allocations are failing segfaults on a Py_DECREF of a NULL filename. setup_context() ...
Open Graph Description: Crash report What happened? AI Disclaimer: this issue was drafted by Claude Code, which also generated the reduced reproducer. Emitting a warning while allocations are failing segfaults on a Py_DEC...
X Description: Crash report What happened? AI Disclaimer: this issue was drafted by Claude Code, which also generated the reduced reproducer. Emitting a warning while allocations are failing segfaults on a Py_DEC...
Opengraph URL: https://github.com/python/cpython/issues/151673
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Segfault: `Py_DECREF(NULL)` in `setup_context`/`do_warn` (`_warnings.c`) when emitting a warning under MemoryError","articleBody":"# Crash report\n\n### What happened?\n\n_AI Disclaimer: this issue was drafted by Claude Code, which also generated the reduced reproducer._\n\nEmitting a warning while allocations are failing segfaults on a `Py_DECREF` of a NULL `filename`. `setup_context()` does not NULL-check `PyUnicode_FromString(\"\u003csys\u003e\")`, and the resulting NULL is later decref'd either by `do_warn()` (success path) or by `setup_context()`'s own `handle_error:` label (which uses `Py_DECREF`, not `Py_XDECREF`).\n\n## Reproducer\n\n```python\nimport _testcapi, warnings, faulthandler\nfaulthandler.enable()\nwarnings.simplefilter(\"always\")\n_testcapi.set_nomemory(0, 0) # fail every allocation from here on\nwarnings.warn(\"boom\") # -\u003e Py_DECREF(NULL) -\u003e SIGSEGV\n```\n\nDeterministic (`start=0`). Reproduced on `main`.\n\n## Backtrace\n\n```\n#0 _Py_atomic_load_uint32_relaxed Include/cpython/pyatomic_gcc.h\n#1 Py_DECREF Include/refcount.h\n#2 do_warn Python/_warnings.c:1139 \u003c- Py_DECREF(filename), filename == NULL\n#3 warnings_warn_impl Python/_warnings.c:1184\n#4 warnings_warn Python/clinic/_warnings.c.h:161\n```\n\n`(gdb) frame 2; print filename` → `$1 = (PyObject *) 0x0`.\n\n## Root cause\n\nIn `setup_context()` (`_warnings.c`), the `f == NULL` branch (~L1036):\n\n```c\nif (f == NULL) {\n globals = interp-\u003esysdict;\n *filename = PyUnicode_FromString(\"\u003csys\u003e\"); /* return value unchecked */\n *lineno = 0;\n}\n```\n\nUnder memory pressure two allocations fail in sequence:\n1. `PyThreadState_GetFrame()` returns NULL (its frame-object allocation fails), so the `f == NULL` branch is taken; then\n2. `PyUnicode_FromString(\"\u003csys\u003e\")` itself returns NULL, leaving `*filename == NULL`.\n\nThe NULL `*filename` then reaches a `Py_DECREF`:\n- if `setup_context()` returns success (registry and `__name__` already present in `globals`, so no further allocation is needed), `do_warn()` runs its cleanup `Py_DECREF(filename)` at L1139 — NULL deref; **or**\n- if `setup_context()` instead hits `handle_error:` (L1084), that path runs `Py_DECREF(*filename)` (L1087, **not** `Py_XDECREF`) — NULL deref.\n\n## Suggested fix\n\n```c\n *filename = PyUnicode_FromString(\"\u003csys\u003e\");\n if (*filename == NULL) {\n goto handle_error;\n }\n```\n\nand at `handle_error:` use `Py_XDECREF(*filename)` (since `*filename` may legitimately be NULL there).\n\n## Notes\n\nFound by OOM-injection fuzzing (`set_nomemory`). Same pattern as gh-146080 (`Py_DECREF(NULL)` in an `_ssl.c` error label). Code is long-standing, so 3.13–3.15 are likely affected as well (unverified).\n\n\nFound using [fusil](https://github.com/devdanzin/fusil) by @vstinner.\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nLinux\n\n### Output from running 'python -VV' on the command line:\n\nPython 3.16.0a0 (heads/main:bfecfcc2a86, Jun 18 2026, 13:48:35) [Clang 22.1.2 (1ubuntu1)]\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-151767\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/devdanzin","@type":"Person","name":"devdanzin"},"datePublished":"2026-06-18T21:06:11.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/151673/cpython/issues/151673"}
| 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:8d3f05cb-5a95-1295-b46f-431e78239696 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B3D0:2677B0:25D3B6:32AE49:6A528A05 |
| html-safe-nonce | 2a92840521c551cc49f12bf1f81bba61b3bde2070ac88607f5538085f42b8697 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCM0QwOjI2NzdCMDoyNUQzQjY6MzJBRTQ5OjZBNTI4QTA1IiwidmlzaXRvcl9pZCI6IjQ2OTY0MzY2MDg0MDgzOTAxNTAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 3c4733fc76b0276e5042a649c6fc1dd0f24fc989ea204f0c4e9db3a3b59464b3 |
| hovercard-subject-tag | issue:4695969429 |
| 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/151673/issue_layout |
| twitter:image | https://opengraph.githubassets.com/01b60f103aa7eff3334f0b9737eae2ba37a127fcf4854ad934a90eb87ffb7d69/python/cpython/issues/151673 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/01b60f103aa7eff3334f0b9737eae2ba37a127fcf4854ad934a90eb87ffb7d69/python/cpython/issues/151673 |
| og:image:alt | Crash report What happened? AI Disclaimer: this issue was drafted by Claude Code, which also generated the reduced reproducer. Emitting a warning while allocations are failing segfaults on a Py_DEC... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | devdanzin |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| 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 | 07a982c1d40157c619b364352b704c3ce66bb332 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width