Skip to content

Conversation

@orenmn
Copy link
Contributor

@orenmn orenmn commented Oct 8, 2017

In addition, add tests to test_xml_etree_c to verify that the crashes are no more.

https://bugs.python.org/issue31728


Py_INCREF(Py_None);
Py_DECREF(JOIN_OBJ(self->text));
_clear_joined_ptr(&self->text);
Copy link
Member

Choose a reason for hiding this comment

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

Setting self->text to NULL is not safe. In this case the "text" getter will return NULL without setting an exception.

elem = cET.Element('elem')
class X:
def __del__(self):
elem.clear()
Copy link
Member

Choose a reason for hiding this comment

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

Getting elem.text or elem.tail before calling elem.clear() will crash.

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I didn't expect the Spanish Inquisition!. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@orenmn
Copy link
Contributor Author

orenmn commented Oct 10, 2017

In the end of element_init(), there are 8 lines that can be replaced by 2 calls to _set_joined_ptr(). I wasn't sure whether this would be an improvement or a code churn, so i didn't change it.

I didn't expect the Spanish Inquisition!

@bedevere-bot
Copy link

Nobody expects the Spanish Inquisition!

@serhiy-storchaka: please review the changes made to this pull request.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

I think this would be an improvement if use the new helper in element_init(), _elementtree_Element___copy___impl(), _elementtree_Element___deepcopy___impl().

It may be worth to move Py_INCREF() out of _set_joined_ptr(). This will help in _elementtree_Element___deepcopy___impl() and will allow to implement _clear_joined_ptr(p) as _set_joined_ptr(p, NULL).

}

/* Py_INCREF followed by Py_SETREF for a PyObject* that uses a join flag. */
static void _set_joined_ptr(PyObject **p, PyObject *new_joined_ptr)
Copy link
Member

Choose a reason for hiding this comment

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

Move static void on a separate line. Or better even use LOCAL(void) or Py_LOCAL_INLINE(void).

* implement _clear_joined_ptr() using _set_joined_ptr().
* use _set_joined_ptr() in more methods.
Py_DECREF(JOIN_OBJ(element->text));
element->text = self->text;
_set_joined_ptr(&element->text, self->text);
Py_INCREF(JOIN_OBJ(element->text));
Copy link
Member

Choose a reason for hiding this comment

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

The new reference should be incremented before decrementing the old reference.

In this particular case the order doesn't matter since the old reference is None, and decrementing it can't trigger executing a user code. But for unifying the code I would move the incrementation above _set_joined_ptr().

_clear_joined_ptr(&self->text);
self->text = text ? JOIN_SET(text, PyList_CheckExact(text)) : Py_None;
Py_INCREF(JOIN_OBJ(self->text));
new_val = text ? JOIN_SET(text, PyList_CheckExact(text)) : Py_None;
Copy link
Member

Choose a reason for hiding this comment

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

You can reuse the text variable for the new value. The old value no longer used.

@serhiy-storchaka
Copy link
Member

And the last comment. The NEWS.d entry should be in the "Library" section.

@serhiy-storchaka serhiy-storchaka merged commit 39ecb9c into python:master Oct 10, 2017
@miss-islington
Copy link
Contributor

Thanks @orenmn for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 2.7, 3.6.
🐍🍒⛏🤖

@miss-islington
Copy link
Contributor

@orenmn and @serhiy-storchaka, Something is wrong... I can't backport for now.
Please backport using cherry_picker on command line.
cherry_picker 39ecb9c71b6e55d8a61a710d0144231bd88f9ada 3.6

@serhiy-storchaka
Copy link
Member

Thank you @orenmn! Do you mind to make backports?

@miss-islington
Copy link
Contributor

@orenmn and @serhiy-storchaka, Something is wrong... I can't backport for now.
Please backport using cherry_picker on command line.
cherry_picker 39ecb9c71b6e55d8a61a710d0144231bd88f9ada 2.7

@miss-islington
Copy link
Contributor

Thanks @orenmn for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 2.7, 3.6.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

@bedevere-bot
Copy link

GH-3945 is a backport of this pull request to the 3.6 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 10, 2017
…p of Element.text and Element.tail (pythonGH-3924)

(cherry picked from commit 39ecb9c)
@miss-islington
Copy link
Contributor

Sorry, @orenmn and @serhiy-storchaka, I could not cleanly backport this to 2.7 due to a conflict.
Please backport using cherry_picker on command line.
cherry_picker 39ecb9c71b6e55d8a61a710d0144231bd88f9ada 2.7

serhiy-storchaka pushed a commit that referenced this pull request Oct 10, 2017
…p of Element.text and Element.tail (GH-3924) (#3945)

(cherry picked from commit 39ecb9c)
@orenmn
Copy link
Contributor Author

orenmn commented Oct 11, 2017

(i am working on a backport to 2.7)

@bedevere-bot
Copy link

GH-3950 is a backport of this pull request to the 2.7 branch.

serhiy-storchaka pushed a commit that referenced this pull request Oct 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type-bug An unexpected behavior, bug, or error

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants