Skip to content

Fix lifetime issue with getAttributeNodeNS() #11422

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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 4 additions & 16 deletions ext/dom/element.c
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ Since: DOM Level 2
PHP_METHOD(DOMElement, getAttributeNodeNS)
{
zval *id;
xmlNodePtr elemp, fakeAttrp;
xmlNodePtr elemp;
xmlAttrPtr attrp;
dom_object *intern;
size_t uri_len, name_len;
Expand All @@ -808,21 +808,9 @@ PHP_METHOD(DOMElement, getAttributeNodeNS)
xmlNsPtr nsptr;
nsptr = dom_get_nsdecl(elemp, (xmlChar *)name);
if (nsptr != NULL) {
xmlNsPtr curns;
curns = xmlNewNs(NULL, nsptr->href, NULL);
if (nsptr->prefix) {
curns->prefix = xmlStrdup((xmlChar *) nsptr->prefix);
}
if (nsptr->prefix) {
fakeAttrp = xmlNewDocNode(elemp->doc, NULL, (xmlChar *) nsptr->prefix, nsptr->href);
} else {
fakeAttrp = xmlNewDocNode(elemp->doc, NULL, (xmlChar *)"xmlns", nsptr->href);
}
fakeAttrp->type = XML_NAMESPACE_DECL;
fakeAttrp->parent = elemp;
fakeAttrp->ns = curns;

DOM_RET_OBJ(fakeAttrp, &ret, intern);
/* Keep parent alive, because we're a fake child. */
GC_ADDREF(&intern->std);
(void) php_dom_create_fake_namespace_decl(elemp, nsptr, return_value, intern);
} else {
RETURN_NULL();
}
Expand Down
20 changes: 20 additions & 0 deletions ext/dom/tests/bug_lifetime_parentNode_getAttributeNodeNS.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
Lifetime issue with parentNode on getAttributeNodeNS()
--EXTENSIONS--
dom
--FILE--
<?php
$xmlString = '<?xml version="1.0" encoding="utf-8" ?>
<root xmlns="http://ns" xmlns:ns2="http://ns2">
<ns2:child />
</root>';

$xml=new DOMDocument();
$xml->loadXML($xmlString);
$ns2 = $xml->documentElement->getAttributeNodeNS("http://www.w3.org/2000/xmlns/", "ns2");
$ns2->parentNode->remove();
var_dump($ns2->parentNode->localName);

?>
--EXPECT--
string(4) "root"