Skip to content

Fix bug #69168: DomNode::getNodePath() returns invalid path #10318

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 2 commits 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
43 changes: 43 additions & 0 deletions ext/xsl/tests/bug69168.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
--TEST--
bug #69168 (DomNode::getNodePath() returns invalid path)
--EXTENSIONS--
xsl
--FILE--
<?php
$xml = <<<EOB
<allusers><user><uid>bob</uid></user><user><uid>joe</uid></user></allusers>
EOB;
$xsl = <<<EOB
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl">
<xsl:template match="allusers">
<xsl:for-each select="user">
<xsl:value-of select="php:function('getPath',uid)"/><br />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
EOB;

function getPath($input){
$input[0]->nodeValue .= 'a';
return $input[0]->getNodePath() . ' = ' . $input[0]->nodeValue;
}

$proc = new XSLTProcessor();
$proc->registerPHPFunctions();
$xslDoc = new DOMDocument();
$xslDoc->loadXML($xsl);
@$proc->importStyleSheet($xslDoc);
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($xml);
echo @$proc->transformToXML($xmlDoc);

// Tests modification of the nodes
var_dump($xmlDoc->firstChild->firstChild->firstChild->getNodePath());
var_dump($xmlDoc->firstChild->firstChild->firstChild->nodeValue);
?>
--EXPECT--
<?xml version="1.0"?>
/allusers/user[1]/uid = boba<br/>/allusers/user[2]/uid = joea<br/>
string(21) "/allusers/user[1]/uid"
string(4) "boba"
14 changes: 13 additions & 1 deletion ext/xsl/xsltprocessor.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,19 @@ static void xsl_ext_function_php(xmlXPathParserContextPtr ctxt, int nargs, int t
node->parent = nsparent;
node->ns = curns;
} else {
node = xmlDocCopyNode(node, domintern->document->ptr, 1);
/**
* Upon freeing libxslt's context, every document which is not the *main* document will be freed by libxslt.
* If a node of a document which is *not the main* document gets returned to userland, we'd free the node twice:
* first by the cleanup of the xslt context, and then by our own refcounting mechanism.
* To prevent this, we'll take a copy if the node is not from the main document.
* It is important that we do not copy the node unconditionally, because that means that:
* - modifications to the node will only modify the copy, and not the original
* - accesses to the parent, path, ... will not work
*/
xsltTransformContextPtr transform_ctxt = (xsltTransformContextPtr) ctxt->context->extra;
if (node->doc != transform_ctxt->document->doc) {
node = xmlDocCopyNode(node, domintern->document->ptr, 1);
}
}

php_dom_create_object(node, &child, domintern);
Expand Down