Skip to content

Commit fbb0061

Browse files
committed
Fix phpGH-16808: Segmentation fault in RecursiveIteratorIterator->current() with a xml element input
When the current data is invalid, NULL must be returned. At least that's how the check in SPL works and how other extensions do this as well. If we don't do this, an UNDEF value gets propagated to a return value (misprinted as null); leading to issues. Closes phpGH-16825.
1 parent 553d79c commit fbb0061

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ PHP NEWS
3737
. Fixed bug GH-16695 (phar:// tar parser and zero-length file header blocks).
3838
(nielsdos, Hans Krentel)
3939

40+
- SimpleXML:
41+
. Fixed bug GH-16808 (Segmentation fault in RecursiveIteratorIterator
42+
->current() with a xml element input). (nielsdos)
43+
4044
21 Nov 2024, PHP 8.2.26
4145

4246
- Cli:

ext/simplexml/simplexml.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2539,7 +2539,11 @@ static zval *php_sxe_iterator_current_data(zend_object_iterator *iter) /* {{{ */
25392539
{
25402540
php_sxe_iterator *iterator = (php_sxe_iterator *)iter;
25412541

2542-
return &iterator->sxe->iter.data;
2542+
zval *data = &iterator->sxe->iter.data;
2543+
if (Z_ISUNDEF_P(data)) {
2544+
return NULL;
2545+
}
2546+
return data;
25432547
}
25442548
/* }}} */
25452549

ext/simplexml/tests/gh16808.phpt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
GH-16808 (Segmentation fault in RecursiveIteratorIterator->current() with a xml element input)
3+
--EXTENSIONS--
4+
simplexml
5+
--FILE--
6+
<?php
7+
$sxe = new SimpleXMLElement("<root />");
8+
$test = new RecursiveIteratorIterator($sxe);
9+
var_dump($test->current());
10+
?>
11+
--EXPECT--
12+
NULL

0 commit comments

Comments
 (0)