Skip to content

Commit f4f34b6

Browse files
committed
Merge branch 'PHP-8.1' into PHP-8.2
* PHP-8.1: Fix GH-12208: SimpleXML infinite loop when a cast is used inside a foreach
2 parents 5df473d + 486276f commit f4f34b6

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
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.2.11
2022

ext/simplexml/simplexml.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,7 +1860,7 @@ static zend_result sxe_object_cast_ex(zend_object *readobj, zval *writeobj, int
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 zend_result sxe_object_cast_ex(zend_object *readobj, zval *writeobj, int
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

Lines changed: 26 additions & 0 deletions
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)