Skip to content

Commit 5bbb760

Browse files
committed
Fix GH-11347: Memory leak when calling a static method inside an xpath query
It's a type confusion bug. `zend_make_callable` may change the function name of the fci to become an array, causing a crash in debug mode on `zval_ptr_dtor_str(&fci.function_name);` in `dom_xpath_ext_function_php`. On a production build it doesn't crash but only causes a leak, because the array elements are not destroyed, only the array container itself is.
1 parent 761b9a4 commit 5bbb760

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

ext/dom/tests/gh11347.phpt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
GH-11347 (Memory leak when calling a static method inside an xpath query)
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
8+
class MyClass
9+
{
10+
public static function dump(string $var) {
11+
var_dump($var);
12+
}
13+
}
14+
15+
$doc = new DOMDocument();
16+
$doc->loadHTML('<a href="https://php.net">hello</a>');
17+
$xpath = new DOMXpath($doc);
18+
$xpath->registerNamespace("php", "http://php.net/xpath");
19+
$xpath->registerPHPFunctions();
20+
$xpath->query("//a[php:function('MyClass::dump', string(@href))]");
21+
22+
?>
23+
Done
24+
--EXPECT--
25+
string(15) "https://php.net"
26+
Done

ext/dom/xpath.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ static void dom_xpath_ext_function_php(xmlXPathParserContextPtr ctxt, int nargs,
182182
}
183183
cleanup:
184184
zend_string_release_ex(callable, 0);
185-
zval_ptr_dtor_str(&fci.function_name);
185+
zval_ptr_dtor(&fci.function_name);
186186
if (fci.param_count > 0) {
187187
for (i = 0; i < nargs - 1; i++) {
188188
zval_ptr_dtor(&fci.params[i]);

0 commit comments

Comments
 (0)