[php-src] master: Merge branch 'PHP-8.4' into PHP-8.5

From: Date: Wed, 28 Oct 2026 07:25:31 +0000
Subject: [php-src] master: Merge branch 'PHP-8.4' into PHP-8.5
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-142870@lists.php.net to get a copy of this message
Author: ndossche (ndossche) Date: 2026-10-28T08:24:57+01:00 Commit: https://github.com/php/php-src/commit/dbd6ed433d884ed25e5c1b3b83dd9dfb88a9c1ee Raw diff: https://github.com/php/php-src/commit/dbd6ed433d884ed25e5c1b3b83dd9dfb88a9c1ee.diff Merge branch 'PHP-8.4' into PHP-8.5 * PHP-8.4: Fix use-after-free in Randomizer::shuffleArray() when the engine throws Changed paths: A ext/random/tests/03_randomizer/methods/shuffleArray_engine_throws_with_holes.phpt M NEWS M ext/standard/array.c Diff: diff --git a/NEWS b/NEWS index 1d13c108d845..0698b387f4a8 100644 --- a/NEWS +++ b/NEWS @@ -206,6 +206,8 @@ PHP NEWS krsort() are still locale-dependent). (Lazizbek Ergashev, Jon Surrell) . Fixed bug GH-24605 (var_export() crashes on deeply nested arrays and objects). (Labish Bardiya) + . Fix use-after-free in Randomizer::shuffleArray() when the engine throws. + (Marti2203) - Streams: . Fixed bug GH-24508 (HTTP wrapper leaks the dechunk filter when header diff --git a/ext/random/tests/03_randomizer/methods/shuffleArray_engine_throws_with_holes.phpt b/ext/random/tests/03_randomizer/methods/shuffleArray_engine_throws_with_holes.phpt new file mode 100644 index 000000000000..b7c0a3b416a2 --- /dev/null +++ b/ext/random/tests/03_randomizer/methods/shuffleArray_engine_throws_with_holes.phpt @@ -0,0 +1,61 @@ +--TEST-- +Random: Randomizer: shuffleArray(): Engine throwing on an array with holes +--FILE-- +<?php + +use Random\Engine; +use Random\Randomizer; + +final class ThrowingEngine implements Engine +{ + public function __construct(private int $calls) {} + + public function generate(): string + { + if ($this->calls-- <= 0) { + throw new Exception('Error'); + } + + return "\x01\x00\x00\x00\x00\x00\x00\x00"; + } +} + +foreach ([0, 1] as $calls) { + $array = [new stdClass, new stdClass, new stdClass, new stdClass]; + unset($array[1]); + + try { + (new Randomizer(new ThrowingEngine($calls)))->shuffleArray($array); + } catch (Exception $e) { + echo $e->getMessage(), PHP_EOL; + } + + var_dump($array); +} + +?> +--EXPECTF-- +Error +array(3) { + [0]=> + object(stdClass)#%d (0) { + } + [2]=> + object(stdClass)#%d (0) { + } + [3]=> + object(stdClass)#%d (0) { + } +} +Error +array(3) { + [0]=> + object(stdClass)#%d (0) { + } + [2]=> + object(stdClass)#%d (0) { + } + [3]=> + object(stdClass)#%d (0) { + } +} diff --git a/ext/standard/array.c b/ext/standard/array.c index c50cba7467bd..db92eeab834a 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -3294,6 +3294,10 @@ PHPAPI bool php_array_data_shuffle(php_random_algo_with_state engine, zval *arra j++; } } + /* Truncate the stale slots left by compaction before the engine can throw. */ + hash->nNumUsed = n_elems; + hash->nInternalPointer = 0; + hash->nNextFreeElement = n_elems; while (--n_left) { rnd_idx = algo->range(state, 0, n_left); if (EG(exception)) { @@ -3322,6 +3326,9 @@ PHPAPI bool php_array_data_shuffle(php_random_algo_with_state engine, zval *arra j++; } } + hash->nNumUsed = n_elems; + hash->nInternalPointer = 0; + hash->nNextFreeElement = n_elems; while (--n_left) { rnd_idx = algo->range(state, 0, n_left); if (EG(exception)) { @@ -3335,9 +3342,6 @@ PHPAPI bool php_array_data_shuffle(php_random_algo_with_state engine, zval *arra } } } - hash->nNumUsed = n_elems; - hash->nInternalPointer = 0; - hash->nNextFreeElement = n_elems; return true; }

« previous php.cvs (#142870) next »