Skip to content

Commit 54c73d2

Browse files
committed
gh-148731: Fix crash in Element.iter() on allocation failure
Initialize parent_stack and parent_stack_used before the fallible PyMem_New call so that elementiter_dealloc does not dereference an uninitialized pointer when the allocation fails.
1 parent a52f428 commit 54c73d2

2 files changed

Lines changed: 5 additions & 1 deletion

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix crash in :meth:`xml.etree.ElementTree.Element.iter` when memory
2+
allocation fails during iterator construction.

Modules/_elementtree.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2368,6 +2368,9 @@ create_elementiter(elementtreestate *st, ElementObject *self, PyObject *tag,
23682368
if (!it)
23692369
return NULL;
23702370

2371+
it->parent_stack = NULL;
2372+
it->parent_stack_used = 0;
2373+
23712374
it->sought_tag = Py_NewRef(tag);
23722375
it->gettext = gettext;
23732376
it->root_element = (ElementObject*)Py_NewRef(self);
@@ -2378,7 +2381,6 @@ create_elementiter(elementtreestate *st, ElementObject *self, PyObject *tag,
23782381
PyErr_NoMemory();
23792382
return NULL;
23802383
}
2381-
it->parent_stack_used = 0;
23822384
it->parent_stack_size = INIT_PARENT_STACK_SIZE;
23832385

23842386
PyObject_GC_Track(it);

0 commit comments

Comments
 (0)