[php-src] master: Merge branch 'PHP-8.6'

From: Date: Wed, 28 Oct 2026 07:25:33 +0000
Subject: [php-src] master: Merge branch 'PHP-8.6'
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-142872@lists.php.net to get a copy of this message
Author: ndossche (ndossche) Date: 2026-10-28T08:25:23+01:00 Commit: https://github.com/php/php-src/commit/8867746907c84a8fcd9d5b3d8bd521fcad8704b4 Raw diff: https://github.com/php/php-src/commit/8867746907c84a8fcd9d5b3d8bd521fcad8704b4.diff Merge branch 'PHP-8.6' * PHP-8.6: 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 ext/standard/array.c Diff: 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 50b45a8f3043..b4ebd8073216 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 (#142872) next »