Skip to content

Change the way how lifetimes work in ext/dom #11576

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 3 commits into from

Conversation

nielsdos
Copy link
Member

@nielsdos nielsdos commented Jul 2, 2023

Fixes GH-9628

Currently, when you remove a node from the DOM, that node will become inaccessible. This even happens when the node is implicitly removed, even when you still hold a reference to it in a variable. This is not possible to detect and prevent easily (see issue details).
This patch changes how lifetimes works. If there's still a userland reference, the node is unlinked instead of freed.
This causes some complications because of how freeing works, especially because of some libxml2 peculiarities (see comments in the code).

nielsdos added 2 commits July 2, 2023 17:42
Previously, a node could be freed even when holding a userland reference
to it. This resulted in exceptions when trying to access that node after
it has been implicitly or explicitly removed. After this patch, a node
will only be freed when the last userland reference disappears.
@@ -2141,7 +2138,7 @@ PHP_METHOD(DOMDocument, registerNodeClass)
}

if (ce == NULL || instanceof_function(ce, basece)) {
DOM_GET_OBJ(docp, id, xmlDocPtr, intern);
DOM_GET_INTERN(id, intern);
Copy link
Member

Choose a reason for hiding this comment

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

Is it me, or is id always just ZEND_THIS ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Most of the time yes. I'll fold that in.

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.

I gave it somewhat a look, and it makes sense to me. But then again I don't really know how the hell libxml2 works.

With the new lifetime system in place, we can simplify some macros.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implicitly removing nodes from \DOMDocument breaks existing references
2 participants