Skip to content

Commit cf70404

Browse files
committed
Fix GH-21583: SimpleXML empty element incorrectly casts to true.
1 parent 9f33bff commit cf70404

3 files changed

Lines changed: 20 additions & 7 deletions

File tree

ext/simplexml/simplexml.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1843,12 +1843,7 @@ static zend_result sxe_object_cast_ex(zend_object *readobj, zval *writeobj, int
18431843
sxe = php_sxe_fetch_object(readobj);
18441844

18451845
if (type == _IS_BOOL) {
1846-
node = php_sxe_get_first_node_non_destructive(sxe, NULL);
1847-
if (node) {
1848-
ZVAL_TRUE(writeobj);
1849-
} else {
1850-
ZVAL_BOOL(writeobj, !sxe_prop_is_empty(readobj));
1851-
}
1846+
ZVAL_BOOL(writeobj, !sxe_prop_is_empty(readobj));
18521847
return SUCCESS;
18531848
}
18541849

ext/simplexml/tests/000.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ object(SimpleXMLElement)#%d (1) {
221221
}
222222
===sxe->elem11->elem111->elem1111
223223
bool(true)
224-
bool(true)
224+
bool(false)
225225
int(1)
226226
object(SimpleXMLElement)#%d (0) {
227227
}

ext/simplexml/tests/gh21583.phpt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
GH-21583 (SimpleXML empty element incorrectly casts to true)
3+
--EXTENSIONS--
4+
simplexml
5+
--FILE--
6+
<?php
7+
$xml = new SimpleXMLElement('<foo><bar/><baz attr="val"/><qux>text</qux></foo>');
8+
9+
var_dump((bool) $xml->bar);
10+
var_dump((bool) $xml->baz);
11+
var_dump((bool) $xml->qux);
12+
var_dump((bool) $xml->nonexistent);
13+
?>
14+
--EXPECT--
15+
bool(false)
16+
bool(true)
17+
bool(true)
18+
bool(false)

0 commit comments

Comments
 (0)