Skip to content

Commit 277cbd6

Browse files
committed
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).
1 parent f5f5059 commit 277cbd6

2 files changed

Lines changed: 4 additions & 1 deletion

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a crash in t-string iteration when creating the iterator fails partway through due to memory pressure.

Objects/templateobject.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ template_iter(PyObject *op)
226226
if (iter == NULL) {
227227
return NULL;
228228
}
229+
iter->stringsiter = NULL;
230+
iter->interpolationsiter = NULL;
231+
iter->from_strings = 1;
229232

230233
PyObject *stringsiter = PyObject_GetIter(self->strings);
231234
if (stringsiter == NULL) {
@@ -242,7 +245,6 @@ template_iter(PyObject *op)
242245

243246
iter->stringsiter = stringsiter;
244247
iter->interpolationsiter = interpolationsiter;
245-
iter->from_strings = 1;
246248
PyObject_GC_Track(iter);
247249
return (PyObject *)iter;
248250
}

0 commit comments

Comments
 (0)