Skip to content

Commit 486276f

Browse files
committedSep 17, 2023
Fix GH-12208: SimpleXML infinite loop when a cast is used inside a foreach
Closes GH-12229.
1 parent 39a9e56 commit 486276f

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed
 

‎NEWS

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ PHP NEWS
1515
within foreach). (nielsdos)
1616
. Fixed bug GH-12223 (Entity reference produces infinite loop in
1717
var_dump/print_r). (nielsdos)
18+
. Fixed bug GH-12208 (SimpleXML infinite loop when a cast is used inside a
19+
foreach). (nielsdos)
1820

1921
28 Sep 2023, PHP 8.1.24
2022

‎ext/simplexml/simplexml.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1860,7 +1860,7 @@ static int sxe_object_cast_ex(zend_object *readobj, zval *writeobj, int type)
18601860
sxe = php_sxe_fetch_object(readobj);
18611861

18621862
if (type == _IS_BOOL) {
1863-
node = php_sxe_get_first_node(sxe, NULL);
1863+
node = php_sxe_get_first_node_non_destructive(sxe, NULL);
18641864
if (node) {
18651865
ZVAL_TRUE(writeobj);
18661866
} else {
@@ -1870,7 +1870,7 @@ static int sxe_object_cast_ex(zend_object *readobj, zval *writeobj, int type)
18701870
}
18711871

18721872
if (sxe->iter.type != SXE_ITER_NONE) {
1873-
node = php_sxe_get_first_node(sxe, NULL);
1873+
node = php_sxe_get_first_node_non_destructive(sxe, NULL);
18741874
if (node) {
18751875
contents = xmlNodeListGetString((xmlDocPtr) sxe->document->ptr, node->children, 1);
18761876
}

‎ext/simplexml/tests/gh12208.phpt

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
GH-12208 (SimpleXML infinite loop when a cast is used inside a foreach)
3+
--EXTENSIONS--
4+
simplexml
5+
--FILE--
6+
<?php
7+
8+
$xml = "<root><a>1</a><a>2</a></root>";
9+
$xml = simplexml_load_string($xml);
10+
11+
$a = $xml->a;
12+
13+
foreach ($a as $test) {
14+
var_dump((string) $a->current());
15+
var_dump((string) $a);
16+
var_dump((bool) $a);
17+
}
18+
19+
?>
20+
--EXPECT--
21+
string(1) "1"
22+
string(1) "1"
23+
bool(true)
24+
string(1) "2"
25+
string(1) "1"
26+
bool(true)

0 commit comments

Comments
 (0)