From 277cbd6acab5a586a36d120a40293608f85505bb Mon Sep 17 00:00:00 2001 From: Vamsi-klu Date: Sun, 26 Jul 2026 07:10:52 +0000 Subject: [PATCH] gh-151815: Fix crash in t-string iterator on partial construction under OOM Initialize templateiter fields to NULL immediately after PyObject_GC_New so tp_clear/dealloc on the error path does not clear garbage pointers (OOM-0024 / gh-151763). --- .../2026-07-26-12-00-01.gh-issue-151815.d4e5f6.rst | 1 + Objects/templateobject.c | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-07-26-12-00-01.gh-issue-151815.d4e5f6.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-07-26-12-00-01.gh-issue-151815.d4e5f6.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-07-26-12-00-01.gh-issue-151815.d4e5f6.rst new file mode 100644 index 000000000000000..66a9edefaf08ad4 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-07-26-12-00-01.gh-issue-151815.d4e5f6.rst @@ -0,0 +1 @@ +Fix a crash in t-string iteration when creating the iterator fails partway through due to memory pressure. diff --git a/Objects/templateobject.c b/Objects/templateobject.c index 1609e82b444516c..806f403bc5fa537 100644 --- a/Objects/templateobject.c +++ b/Objects/templateobject.c @@ -226,6 +226,9 @@ template_iter(PyObject *op) if (iter == NULL) { return NULL; } + iter->stringsiter = NULL; + iter->interpolationsiter = NULL; + iter->from_strings = 1; PyObject *stringsiter = PyObject_GetIter(self->strings); if (stringsiter == NULL) { @@ -242,7 +245,6 @@ template_iter(PyObject *op) iter->stringsiter = stringsiter; iter->interpolationsiter = interpolationsiter; - iter->from_strings = 1; PyObject_GC_Track(iter); return (PyObject *)iter; }