GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-146056: Fix TreeBuilder stack in xml.etree by vstinner · Pull Request #146062 · python/cpython

Conversation

@vstinner

@vstinner vstinner commented Mar 17, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

No longer create a stack of 20 items, but create an empty stack instead. It prevents crashes when the stack list is discovered by gc.get_referrers() or other functions.

Fix also reference counting in treebuilder_handle_end().

  • Issue: Crash in repr() for lists containing NULLs #146056

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@vstinner
pythongh-146056: Fix TreeBuilder stack in xml.etree
8d2d68f
No longer create a stack of 20 items, but create an empty stack
instead. It prevents crashes when the stack list is discovered by
gc.get_referrers() or other functions.

Fix also reference counting in treebuilder_handle_end().
@vstinner vstinner added the skip news label Mar 17, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Mar 17, 2026
Crash in repr() for lists containing NULLs #146056
Closed
@bedevere-app bedevere-app Bot added the awaiting core review label Mar 17, 2026
@vstinner

vstinner commented Mar 17, 2026

Copy link
Copy Markdown
Member Author

I added "skip news" since this issue requires calling gc.get_referrers() which is used in "regular code".

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka
serhiy-storchaka requested review from serhiy-storchaka March 18, 2026 15:58
serhiy-storchaka
serhiy-storchaka reviewed Mar 18, 2026
View reviewed changes
Comment thread Modules/_elementtree.c
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));

@serhiy-storchaka serhiy-storchaka Mar 18, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@vstinner vstinner Mar 19, 2026

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I wrote #146167 which uses your suggestion.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Modules/_elementtree.c
t->comment_factory = NULL;
t->pi_factory = NULL;
t->stack = PyList_New(20);
t->stack = PyList_New(0);

@serhiy-storchaka serhiy-storchaka Mar 18, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

This can have performance impact.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@vstinner

vstinner commented Mar 19, 2026

Copy link
Copy Markdown
Member Author

I abandon my change since a more generic change was merged: #146129.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner vstinner closed this Mar 19, 2026
@vstinner
vstinner deleted the fix_etree_stack branch July 10, 2026 20:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@serhiy-storchaka serhiy-storchaka Awaiting requested review from serhiy-storchaka

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting core review skip news

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@vstinner @serhiy-storchaka
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.