Skip to content

Fix GH-12231: SimpleXML xpath should warn when returning other return types than node lists #18073

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
5 changes: 5 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ PHP 8.5 UPGRADE NOTES
. A ValueError is now thrown when trying to set a cursor name that is too
long on a PDOStatement resulting from the Firebird driver.

- SimpleXML:
- Passing an XPath expression that returns something other than a node set
to SimpleXMLElement::xpath() will now emit a warning and return false,
instead of silently failing and returning an empty array.

- SPL:
. ArrayObject no longer accepts enums, as modifying the $name or $value
properties can break engine assumptions.
Expand Down
22 changes: 22 additions & 0 deletions ext/simplexml/simplexml.c
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,21 @@ static int sxe_objects_compare(zval *object1, zval *object2) /* {{{ */
}
/* }}} */

static const char *sxe_get_object_type_name(xmlXPathObjectType type)
{
switch (type) {
case XPATH_BOOLEAN: return "bool";
case XPATH_NUMBER: return "number";
case XPATH_STRING: return "string";
#ifdef LIBXML_XPTR_LOCS_ENABLED
case XPATH_POINT: return "point";
case XPATH_RANGE: return "range";
case XPATH_LOCATIONSET: return "location set";
#endif
default: return "undefined";
}
}

/* {{{ Runs XPath query on the XML data */
PHP_METHOD(SimpleXMLElement, xpath)
{
Expand Down Expand Up @@ -1271,6 +1286,13 @@ PHP_METHOD(SimpleXMLElement, xpath)
RETURN_FALSE;
}

if (UNEXPECTED(retval->type != XPATH_NODESET)) {
php_error_docref(NULL, E_WARNING, "XPath expression must return a node set, %s returned",
sxe_get_object_type_name(retval->type));
xmlXPathFreeObject(retval);
RETURN_FALSE;
}

result = retval->nodesetval;

if (result != NULL) {
Expand Down
7 changes: 4 additions & 3 deletions ext/simplexml/tests/008.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ array(1) {
}
}
}
array(0) {
}

Warning: SimpleXMLElement::xpath(): Invalid expression in %s on line %d%A
Warning: SimpleXMLElement::xpath(): XPath expression must return a node set, number returned in %s on line %d
bool(false)

Warning: SimpleXMLElement::xpath(): Invalid expression in %s on line %d
bool(false)
26 changes: 26 additions & 0 deletions ext/simplexml/tests/gh12231.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
GH-12231 (SimpleXML xpath should warn when returning other return types than node lists)
--EXTENSIONS--
simplexml
--FILE--
<?php

$xml = "<container><foo/><foo/></container>";
$sxe = simplexml_load_string($xml);

var_dump($sxe->xpath("count(//foo)"));
var_dump($sxe->xpath("string(//foo)"));
var_dump($sxe->xpath("boolean(//foo)"));
var_dump(count($sxe->xpath("//foo")));

?>
--EXPECTF--
Warning: SimpleXMLElement::xpath(): XPath expression must return a node set, number returned in %s on line %d
bool(false)

Warning: SimpleXMLElement::xpath(): XPath expression must return a node set, string returned in %s on line %d
bool(false)

Warning: SimpleXMLElement::xpath(): XPath expression must return a node set, bool returned in %s on line %d
bool(false)
int(2)
Loading