Skip to content

Fix #80927: Removing documentElement after creating attribute node: p… #11892

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 Aug 6, 2023

…ossible use-after-free

Not 100% convinced that this solution is optimal. Also targeting master only because this is a non-trivial change in how the destruction works.
Marking as draft for now until I convince myself more that there's no better alternative.

@nielsdos
Copy link
Member Author

nielsdos commented Aug 8, 2023

I don't see another way to solve this issue ATM.

The main problem is that libxml2 doesn't have a reference count on the namespace declaration. I understand why this is, it's non-trivial to keep track of (e.g. clones, imports, adoptions, ...) and normally it doesn't matter.

We don't actually need to save the namespace declaration if we know the subtree it belongs to has no references from userland. However, we can't know that without traversing the whole subtree (=> slow), or without adding some subtree metadata (=> also slow). So we have to assume we need to save everything.

However, namespace declarations are quite rare in comparison to other node types. Most node types are either elements, text or attributes. And you only need one namespace declaration per namespace (in principle). So I expect the number of namespace declarations to be low for an average XML document.

In the worst possible case we have to save all namespace declarations when we for example remove the whole document. But given the above reasoning this likely won't be a lot of declarations even in the worst case.
A single declaration only takes about 48 bytes of memory, and I don't expect the worst case to occur very often (why would you remove the whole document?).
So I guess this is fine (but still ugly).

I'm going to mark this as ready, and update the comment in the code to include some of this text for future reference.

@nielsdos nielsdos marked this pull request as ready for review August 8, 2023 19:01
@nielsdos nielsdos requested a review from Girgias August 12, 2023 10:05
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.

LGTM, just minor comments that can be ignored.

var_dump($a->prefix);
}

function test2($variation) {
Copy link
Member

Choose a reason for hiding this comment

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

You could use an Enum and type against this for the variation arg

php_libxml_set_old_ns_list(node->doc, ns, last);
node->nsDef = NULL;
}
ZEND_FALLTHROUGH;
Copy link
Member

Choose a reason for hiding this comment

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

I get that this how it was previously written, and the default case is very small and clear. But I wonder if it just doesn't make more sense to keep it self-contained and have:

				xmlFreeNode(node);
				break;

here too.

@nielsdos nielsdos closed this in bb092ab Aug 12, 2023
ju1ius pushed a commit to ju1ius/php-src that referenced this pull request Aug 15, 2023
jorgsowa pushed a commit to jorgsowa/php-src that referenced this pull request Aug 16, 2023
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.

2 participants