Skip to content

Commit 657167f

Browse files
committed
Destroy xpath callbacks at the time when the reconstruction happens
1 parent 01f1c60 commit 657167f

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
--TEST--
2+
DOMXPath: Calling __construct() again when functions were already registered
3+
--EXTENSIONS--
4+
dom
5+
--SKIPIF--
6+
<?php
7+
if (!class_exists('DOMXPath')) die('skip no xpath support');
8+
?>
9+
--FILE--
10+
<?php
11+
12+
$dom = new DOMDocument;
13+
$dom->loadXML('<root/>');
14+
15+
class Test {
16+
public function __destruct() {
17+
echo "destruct\n";
18+
}
19+
20+
public function test() {
21+
echo "test\n";
22+
}
23+
}
24+
25+
echo "=== First run ===\n";
26+
27+
$xpath = new DOMXPath($dom);
28+
$xpath->registerNamespace('foo', 'urn:foo');
29+
$xpath->registerPhpFunctionNS('urn:foo', 'test', [new Test, 'test']);
30+
31+
echo "=== Reconstruct ===\n";
32+
33+
$xpath->__construct($dom, true);
34+
35+
echo "=== Second run ===\n";
36+
37+
$xpath->registerNamespace('foo', 'urn:foo');
38+
$xpath->query('//*[foo:test()]');
39+
$xpath->registerPhpFunctionNS('urn:foo', 'test', [new Test, 'test']);
40+
$xpath->query('//*[foo:test()]');
41+
42+
?>
43+
--EXPECTF--
44+
=== First run ===
45+
=== Reconstruct ===
46+
destruct
47+
=== Second run ===
48+
49+
Warning: DOMXPath::query(): xmlXPathCompOpEval: function test not found in %s on line %d
50+
51+
Warning: DOMXPath::query(): Unregistered function in %s on line %d
52+
test
53+
destruct

ext/dom/xpath.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ PHP_METHOD(DOMXPath, __construct)
137137
if (oldctx != NULL) {
138138
php_libxml_decrement_doc_ref((php_libxml_node_object *) &intern->dom);
139139
xmlXPathFreeContext(oldctx);
140+
php_dom_xpath_callbacks_dtor(&intern->xpath_callbacks);
141+
php_dom_xpath_callbacks_ctor(&intern->xpath_callbacks);
140142
}
141143

142144
xmlXPathRegisterFuncNS (ctx, (const xmlChar *) "functionString",

ext/dom/xpath_callbacks.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ PHP_DOM_EXPORT void php_dom_xpath_callback_ns_dtor(php_dom_xpath_callback_ns *ns
4646

4747
PHP_DOM_EXPORT void php_dom_xpath_callbacks_ctor(php_dom_xpath_callbacks *registry)
4848
{
49+
registry->php_ns = NULL;
50+
registry->namespaces = NULL;
51+
registry->node_list = NULL;
4952
}
5053

5154
PHP_DOM_EXPORT void php_dom_xpath_callbacks_clean_node_list(php_dom_xpath_callbacks *registry)

0 commit comments

Comments
 (0)