Skip to content

Commit 8e00e46

Browse files
committed
Fix GH-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.
1 parent 4124b04 commit 8e00e46

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

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)