Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion Lib/test/test_xml_etree_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import io
import struct
from test import support
from test.support.import_helper import import_fresh_module
from test.support.import_helper import import_fresh_module, import_module
import types
import unittest

Expand All @@ -15,6 +15,10 @@

@unittest.skipUnless(cET, 'requires _elementtree')
class MiscTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.testcapi = import_module('_testcapi')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is not available the entire MiscTests will be skipped?


# Issue #8651.
@support.bigmemtest(size=support._2G + 100, memuse=1, dry_run=False)
def test_length_overflow(self, size):
Expand Down Expand Up @@ -183,6 +187,23 @@ def __hash__(self):
r = e.get(X())
self.assertIsNone(r)

@unittest.skipIf(support.Py_TRACE_REFS,
'Py_TRACE_REFS conflicts with testcapi.set_nomemory')
def test_iter_oom_no_crash(self):
# gh-148731: OOM while creating an Element iterator should raise
# MemoryError without crashing while deallocating a partially
# initialized iterator object.
element = cET.Element('root')
raised = False
self.testcapi.set_nomemory(1, 2)
try:
element.iter()
except MemoryError:
raised = True
finally:
self.testcapi.remove_mem_hooks()
self.assertTrue(raised, "MemoryError not raised")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use:

with self.assertRaises(MemoryError):

inside the try instead.


@support.cpython_only
def test_immutable_types(self):
root = cET.fromstring('<a></a>')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix a crash in :mod:`xml.etree.ElementTree` when
:meth:`~xml.etree.ElementTree.Element.iter` fails with :exc:`MemoryError`
while creating the C accelerator iterator.
7 changes: 7 additions & 0 deletions Modules/_elementtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -2359,6 +2359,13 @@ create_elementiter(elementtreestate *st, ElementObject *self, PyObject *tag,
if (!it)
return NULL;

it->parent_stack = NULL;
it->parent_stack_used = 0;
it->parent_stack_size = 0;
it->root_element = NULL;
it->sought_tag = NULL;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are initialised shortly after anyway?

it->gettext = 0;

it->sought_tag = Py_NewRef(tag);
it->gettext = gettext;
it->root_element = (ElementObject*)Py_NewRef(self);
Expand Down
Loading