FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-146056: Fix TreeBuilder stack in xml.etree by vstinner · Pull Request #146062 · python/cpython · GitHub

/ cpython Public
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
18 changes: 18 additions & 0 deletions Lib/test/test_xml_etree_c.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# xml.etree test for cElementTree
import gc
import io
import struct
from test import support
Expand Down Expand Up @@ -203,6 +204,23 @@ def test_disallow_instantiation(self):
iter_type = type(root.iter())
support.check_disallow_instantiation(self, iter_type)

def test_tree_builder_stack(self):
builder = cET.TreeBuilder()
for x in range(10):
builder.start("a", {})
for x in range(10):
builder.end("a")
root = builder.close()

# Find the internal TreeBuilder stack list using gc.get_referrers()
for ref in gc.get_referrers(root[0]):
if isinstance(ref, list):
# Check that the list doesn't contain NULL items (gh-146056)
repr(ref)
break
else:
self.fail("failed to find TreeBuilder stack list")


@unittest.skipUnless(cET, 'requires _elementtree')
class TestAliasWorking(unittest.TestCase):
Expand Down
6 changes: 3 additions & 3 deletions Modules/_elementtree.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2431,7 +2431,7 @@ treebuilder_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
t->element_factory = NULL;
t->comment_factory = NULL;
t->pi_factory = NULL;
t->stack = PyList_New(20);
t->stack = PyList_New(0);

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This can have performance impact.

if (!t->stack) {
Py_DECREF(t->this);
Py_DECREF(t->last);
Expand Down Expand Up @@ -2856,9 +2856,9 @@ treebuilder_handle_end(TreeBuilderObject* self, PyObject* tag)

item = self->last;
self->last = Py_NewRef(self->this);
Py_XSETREF(self->last_for_tail, self->last);
Py_XSETREF(self->last_for_tail, Py_NewRef(self->last));

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This is unrelated.

If rewrite this code to be more explicit or safe, I would write something like

    PyObject *last = self->last;
    PyObject *last_for_tail = self->last_for_tail;
    PyObject *this = self->this;
    self->index--;
    self->this = Py_NewRef(PyList_GET_ITEM(self->stack, self->index));
    self->last = Py_NewRef(this);
    self->last_for_tail = Py_NewRef(this);
    Py_DECREF(last);
    Py_XDECREF(last_for_tail);

    if (treebuilder_append_event(self, self->end_event_obj, this) < 0) {
        Py_DECREF(this);
        return NULL;
    }

    return this;

But we should also look a the other ends -- how these attributes are set in other code in this file. This is a separate issue.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I wrote #146167 which uses your suggestion.

self->index--;
self->this = Py_NewRef(PyList_GET_ITEM(self->stack, self->index));
Py_SETREF(self->this, Py_NewRef(PyList_GET_ITEM(self->stack, self->index)));
Py_DECREF(item);

if (treebuilder_append_event(self, self->end_event_obj, self->last) < 0)
Expand Down
Loading

Back | FazBrowse Home | New Git URL