Skip to content

Fix DOMElement::append() and DOMElement::prepend() hierarchy checks #11344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

nielsdos
Copy link
Member

@nielsdos nielsdos commented May 29, 2023

We could end up in an invalid hierarchy, resulting in infinite loops and eventual crashes if we don't check for the DOM hierarchy validity.
This is in accordance to the "pre-insertion validity" check from spec.

Requesting a review from @Girgias again (sorry 😅) since no one else seems to be able to review these atm.

We could end up in an invalid hierarchy, resulting in infinite loops and
eventual crashes if we don't check for the DOM hierarchy validity.
@nielsdos nielsdos requested a review from Girgias May 29, 2023 20:42
Copy link
Member

@Girgias Girgias left a comment

Choose a reason for hiding this comment

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

Looks sensible. maybe just as a follow-up in master to convert the return type of dom_hierarchy() to zend_result.

@@ -296,6 +319,11 @@ void dom_parent_node_prepend(dom_object *context, zval *nodes, int nodesc)
return;
}

if (UNEXPECTED(dom_hierarchy_node_list(parentNode, nodes, nodesc) != SUCCESS)) {
Copy link
Member

Choose a reason for hiding this comment

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

Any reason for using != SUCCESS? Shouldn't really be an issue here but when I was converting a lot of int return values to zend_result this code pattern always made me triple check that I was not missing a case where something other than SUCCESS and FAILURE was returned.

Suggested change
if (UNEXPECTED(dom_hierarchy_node_list(parentNode, nodes, nodesc) != SUCCESS)) {
if (UNEXPECTED(dom_hierarchy_node_list(parentNode, nodes, nodesc) == FAILURE)) {

Copy link
Member Author

Choose a reason for hiding this comment

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

I have a habit of doing != SUCCESS because it's a comparison against 0, which encodes as a shorter instruction than a comparison with -1. I saw both != SUCCESS and == FAILURE in the code so I just picked the one I liked most.
I can change it if you want?

Copy link
Member

Choose a reason for hiding this comment

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

Either is fine frankly, I think I was told about the shorter instruction once.

I believe that one day zend_result can be a true alias for bool... but that's a lot of code auditing.

static zend_result dom_hierarchy_node_list(xmlNodePtr parentNode, zval *nodes, int nodesc)
{
for (int i = 0; i < nodesc; i++) {
if (Z_TYPE(nodes[i]) == IS_OBJECT) {
Copy link
Member

Choose a reason for hiding this comment

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

Wait, can a node not be an object?

Copy link
Member Author

Choose a reason for hiding this comment

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

Spec says you may insert strings, so it can be IS_STRING too. In that case we create a text node, but since that's a new object it cannot violate the dom hierarchy.

Copy link
Member

Choose a reason for hiding this comment

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

Right, I didn't think that the zval node list could contain anything other than objects, as I would have expected free strings to always be encoded as DOMText object. Just having wrong assumptions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants