You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Segfault: Py_DECREF(NULL) in setup_context/do_warn (_warnings.c) when emitting a warning under MemoryError #151673
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() does not NULL-check PyUnicode_FromString("<sys>"), 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).
Reproducer
import_testcapi, warnings, faulthandlerfaulthandler.enable()
warnings.simplefilter("always")
_testcapi.set_nomemory(0, 0) # fail every allocation from here onwarnings.warn("boom") # -> Py_DECREF(NULL) -> SIGSEGV
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
if setup_context() instead hits handle_error: (L1084), that path runs Py_DECREF(*filename) (L1087, notPy_XDECREF) — NULL deref.
Suggested fix
*filename=PyUnicode_FromString("<sys>");
if (*filename==NULL) {
goto handle_error;
}
and at handle_error: use Py_XDECREF(*filename) (since *filename may legitimately be NULL there).
Notes
Found 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).
Activity
pythongh-151673: Fix crash in warnings.warn() under memory pressure