Skip to content

Commit b3f5bb5

Browse files
committed
Fix SimpleXML empty element boolean cast
Use the existing SimpleXML emptiness check for boolean casts instead of returning true for any existing node. This makes empty elements without attributes cast to false while keeping elements with text, attributes, or children truthy.
1 parent bba2dc3 commit b3f5bb5

3 files changed

Lines changed: 33 additions & 7 deletions

File tree

ext/simplexml/simplexml.c

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

18411841
if (type == _IS_BOOL) {
1842-
node = php_sxe_get_first_node_non_destructive(sxe, NULL);
1843-
if (node) {
1844-
ZVAL_TRUE(writeobj);
1845-
} else {
1846-
ZVAL_BOOL(writeobj, !sxe_prop_is_empty(readobj));
1847-
}
1842+
ZVAL_BOOL(writeobj, !sxe_prop_is_empty(readobj));
18481843
return SUCCESS;
18491844
}
18501845

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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
GH-21583 (empty SimpleXML child element casts to false)
3+
--EXTENSIONS--
4+
simplexml
5+
--FILE--
6+
<?php
7+
8+
$xml = simplexml_load_string(<<<XML
9+
<root>
10+
<empty/>
11+
<with-text>content</with-text>
12+
<with-attribute value="1"/>
13+
<with-child><child/></with-child>
14+
</root>
15+
XML);
16+
17+
var_dump((bool) $xml->empty);
18+
var_dump((bool) $xml->missing);
19+
var_dump((bool) $xml->{"with-text"});
20+
var_dump((bool) $xml->{"with-attribute"});
21+
var_dump((bool) $xml->{"with-child"});
22+
var_dump((bool) simplexml_load_string('<root/>'));
23+
24+
?>
25+
--EXPECT--
26+
bool(false)
27+
bool(false)
28+
bool(true)
29+
bool(true)
30+
bool(true)
31+
bool(false)

0 commit comments

Comments
 (0)