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

From: Date: Wed, 28 Oct 2026 07:25:31 +0000
Subject: [php-src] PHP-8.6: Merge branch 'PHP-8.5' into PHP-8.6
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-142871@lists.php.net to get a copy of this message
Author: ndossche (ndossche) Date: 2026-10-28T08:25:17+01:00 Commit: https://github.com/php/php-src/commit/30bcf4bd89a7f3d31753ccaeb8dd10990507558a Raw diff: https://github.com/php/php-src/commit/30bcf4bd89a7f3d31753ccaeb8dd10990507558a.diff Merge branch 'PHP-8.5' into PHP-8.6 * PHP-8.5: 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 d06328e8324b..42aabc577320 100644 --- a/NEWS +++ b/NEWS @@ -106,6 +106,8 @@ PHP NEWS was not opened by PHP). (lazerg) . Fixed bug GH-24552 (Case-insensitive substr_compare(), ksort() and krsort() are still locale-dependent). (Lazizbek Ergashev, Jon Surrell) + . Fix use-after-free in Randomizer::shuffleArray() when the engine throws. + (Marti2203) - Streams: . Fixed bug GH-24506 (stream_select() leaks a stream-error operation level 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 e406b45dfcd5..e9b06461325c 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -3193,6 +3193,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)) { @@ -3221,6 +3225,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)) { @@ -3234,9 +3241,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 (#142871) next »