Skip to content
Open
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
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ PHP NEWS
. Fixed bug GH-15375 (Nested "yield from" skips items after a valid() or
next() call on the inner generator). (iliaal)

- DOM:
. Fixed a crash in DOMXPath when a php:function callback receives a nodeset
and a later callback returns a node from another document. (iliaal)

- Readline:
. Fixed the interactive shell not waiting for the pager process to exit.
(Weilin Du)
Expand Down
40 changes: 40 additions & 0 deletions ext/dom/tests/xpath_php_function_foreign_doc.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--TEST--
DOMXPath: php:function nodeset args plus a foreign-document return must not treat arrays as DOM objects
--EXTENSIONS--
dom
--FILE--
<?php
$doc1 = new DOMDocument();
$doc1->loadXML('<root><a>1</a></root>');
$doc2 = new DOMDocument();
$doc2->loadXML('<root><b>2</b></root>');

$xp = new DOMXPath($doc1);
$xp->registerNamespace('php', 'http://php.net/xpath');
$xp->registerPhpFunctions();

function uses_nodeset($nodes) {
return true;
}

function foreign() {
global $doc2;
return $doc2->documentElement;
}

$xp->query('//a[php:function("uses_nodeset", //a)]');
$res = $xp->query('php:function("foreign")');
echo "count: ";
var_dump($res->length);
$n = $res->item(0);
echo "name: ";
var_dump($n->nodeName);
echo "owner is doc2: ";
var_dump($n->ownerDocument === $doc2);
echo "done\n";
?>
--EXPECT--
count: int(1)
name: string(4) "root"
owner is doc2: bool(true)
done
25 changes: 22 additions & 3 deletions ext/dom/xpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,25 @@

#ifdef LIBXML_XPATH_ENABLED

static dom_object *dom_xpath_intern_from_entry(zval *entry, xmlDocPtr doc)
{
if (Z_TYPE_P(entry) == IS_OBJECT) {
dom_object *obj = Z_DOMOBJ_P(entry);
if (obj->document && obj->document->ptr == doc) {
return obj;
}
} else if (Z_TYPE_P(entry) == IS_ARRAY) {
zval *inner;
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(entry), inner) {
dom_object *obj = dom_xpath_intern_from_entry(inner, doc);
if (obj) {
return obj;
}
} ZEND_HASH_FOREACH_END();
}
return NULL;
}

static dom_object *dom_xpath_intern_for_doc(dom_xpath_object *xpath_obj, xmlDocPtr doc)
{
if (xpath_obj->dom.document && xpath_obj->dom.document->ptr == doc) {
Expand All @@ -43,9 +62,9 @@ static dom_object *dom_xpath_intern_for_doc(dom_xpath_object *xpath_obj, xmlDocP
HashTable *node_list = xpath_obj->xpath_callbacks.node_list;
if (node_list) {
zval *entry;
ZEND_HASH_PACKED_FOREACH_VAL(node_list, entry) {
dom_object *obj = Z_DOMOBJ_P(entry);
if (obj->document && obj->document->ptr == doc) {
ZEND_HASH_FOREACH_VAL(node_list, entry) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

what s the reason of this change?

dom_object *obj = dom_xpath_intern_from_entry(entry, doc);
if (obj) {
return obj;
}
} ZEND_HASH_FOREACH_END();
Expand Down
Loading