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.

Closes GH-16825.
This commit is contained in:
Niels Dossche 2024-11-15 21:38:27 +01:00
parent 553d79c709
commit fbb0061993
No known key found for this signature in database
GPG Key ID: B8A8AD166DF0E2E5
3 changed files with 21 additions and 1 deletions

4
NEWS
View File

@ -37,6 +37,10 @@ PHP NEWS
. Fixed bug GH-16695 (phar:// tar parser and zero-length file header blocks).
(nielsdos, Hans Krentel)
- SimpleXML:
. Fixed bug GH-16808 (Segmentation fault in RecursiveIteratorIterator
->current() with a xml element input). (nielsdos)
21 Nov 2024, PHP 8.2.26
- Cli:

View File

@ -2539,7 +2539,11 @@ static zval *php_sxe_iterator_current_data(zend_object_iterator *iter) /* {{{ */
{
php_sxe_iterator *iterator = (php_sxe_iterator *)iter;
return &iterator->sxe->iter.data;
zval *data = &iterator->sxe->iter.data;
if (Z_ISUNDEF_P(data)) {
return NULL;
}
return data;
}
/* }}} */

View File

@ -0,0 +1,12 @@
--TEST--
GH-16808 (Segmentation fault in RecursiveIteratorIterator->current() with a xml element input)
--EXTENSIONS--
simplexml
--FILE--
<?php
$sxe = new SimpleXMLElement("<root />");
$test = new RecursiveIteratorIterator($sxe);
var_dump($test->current());
?>
--EXPECT--
NULL