Skip to content

Commit 1e96fbe

Browse files
author
Rob Richards
committed
fix bug #48601 (xpath() returns FALSE for legitimate query)
add test
1 parent 6597e85 commit 1e96fbe

3 files changed

Lines changed: 40 additions & 21 deletions

File tree

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ PHP NEWS
137137
- Fixed bug #48983 (DomDocument : saveHTMLFile wrong charset). (Rob)
138138
- Fixed bug #48902 (Timezone database fallback map is outdated). (Derick)
139139
- Fixed bug #48781 (Cyclical garbage collector memory leak). (Dmitry)
140+
- Fixed bug #48601 (xpath() returns FALSE for legitimate query). (Rob)
140141
- Fixed bug #48361 (SplFileInfo::getPathInfo should return the
141142
parent dir). (Etienne)
142143
- Fixed bug #48289 (iconv_mime_encode() quoted-printable scheme is broken).

ext/simplexml/simplexml.c

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,31 +1258,29 @@ SXE_METHOD(xpath)
12581258
}
12591259

12601260
result = retval->nodesetval;
1261-
if (!result) {
1262-
xmlXPathFreeObject(retval);
1263-
RETURN_FALSE;
1264-
}
12651261

12661262
array_init(return_value);
12671263

1268-
for (i = 0; i < result->nodeNr; ++i) {
1269-
nodeptr = result->nodeTab[i];
1270-
if (nodeptr->type == XML_TEXT_NODE || nodeptr->type == XML_ELEMENT_NODE || nodeptr->type == XML_ATTRIBUTE_NODE) {
1271-
MAKE_STD_ZVAL(value);
1272-
/**
1273-
* Detect the case where the last selector is text(), simplexml
1274-
* always accesses the text() child by default, therefore we assign
1275-
* to the parent node.
1276-
*/
1277-
if (nodeptr->type == XML_TEXT_NODE) {
1278-
_node_as_zval(sxe, nodeptr->parent, value, SXE_ITER_NONE, NULL, NULL, 0 TSRMLS_CC);
1279-
} else if (nodeptr->type == XML_ATTRIBUTE_NODE) {
1280-
_node_as_zval(sxe, nodeptr->parent, value, SXE_ITER_ATTRLIST, (char*)nodeptr->name, nodeptr->ns ? (xmlChar *)nodeptr->ns->href : NULL, 0 TSRMLS_CC);
1281-
} else {
1282-
_node_as_zval(sxe, nodeptr, value, SXE_ITER_NONE, NULL, NULL, 0 TSRMLS_CC);
1283-
}
1264+
if (result != NULL) {
1265+
for (i = 0; i < result->nodeNr; ++i) {
1266+
nodeptr = result->nodeTab[i];
1267+
if (nodeptr->type == XML_TEXT_NODE || nodeptr->type == XML_ELEMENT_NODE || nodeptr->type == XML_ATTRIBUTE_NODE) {
1268+
MAKE_STD_ZVAL(value);
1269+
/**
1270+
* Detect the case where the last selector is text(), simplexml
1271+
* always accesses the text() child by default, therefore we assign
1272+
* to the parent node.
1273+
*/
1274+
if (nodeptr->type == XML_TEXT_NODE) {
1275+
_node_as_zval(sxe, nodeptr->parent, value, SXE_ITER_NONE, NULL, NULL, 0 TSRMLS_CC);
1276+
} else if (nodeptr->type == XML_ATTRIBUTE_NODE) {
1277+
_node_as_zval(sxe, nodeptr->parent, value, SXE_ITER_ATTRLIST, (char*)nodeptr->name, nodeptr->ns ? (xmlChar *)nodeptr->ns->href : NULL, 0 TSRMLS_CC);
1278+
} else {
1279+
_node_as_zval(sxe, nodeptr, value, SXE_ITER_NONE, NULL, NULL, 0 TSRMLS_CC);
1280+
}
12841281

1285-
add_next_index_zval(return_value, value);
1282+
add_next_index_zval(return_value, value);
1283+
}
12861284
}
12871285
}
12881286

ext/simplexml/tests/bug48601.phpt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Bug #48601 (xpath() returns FALSE for legitimate query)
3+
--SKIPIF--
4+
<?php if (!extension_loaded("simplexml")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
8+
$sxe = simplexml_load_string('<root><node1>1</node1></root>');
9+
10+
$nodes = $sxe->xpath("/root/node2/@test");
11+
12+
if (! is_array($nodes)) {
13+
echo "An error occured\n";
14+
} else {
15+
echo "Result Count: " . count($nodes) . "\n";
16+
}
17+
18+
?>
19+
--EXPECTF--
20+
Result Count: 0

0 commit comments

Comments
 (0)