[php-src] PHP-8.5: Fix use-after-free in Randomizer::shuffleArray() when the engine throws
| From: | Martin Mirchev via ndossche | Date: | Wed, 28 Oct 2026 07:25:30 +0000 |
| Subject: | [php-src] PHP-8.5: Fix use-after-free in Randomizer::shuffleArray() when the engine throws | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-142869@lists.php.net to get a copy of this message | ||
Author: Martin Mirchev (Marti2203)
Committer: ndossche (ndossche)
Date: 2026-10-28T08:24:25+01:00
Commit: https://github.com/php/php-src/commit/3a8bcb6d186b4445e47de9adf8f44be5a48536af
Raw diff: https://github.com/php/php-src/commit/3a8bcb6d186b4445e47de9adf8f44be5a48536af.diff
Fix use-after-free in Randomizer::shuffleArray() when the engine throws
php_array_data_shuffle() first compacts the holes of a packed array and
only then updates nNumUsed. When the engine threw in between, the stale
tail slots were still counted, so the moved values were released twice
when the copied array was destroyed. Update nNumUsed right after the
compaction.
Closes GH-24553.
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 79fdd4b7ef67..6b667b6bb169 100644
--- a/NEWS
+++ b/NEWS
@@ -229,6 +229,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 82c3f0a7aa6e..c10ad47c093f 100644
--- a/ext/standard/array.c
+++ b/ext/standard/array.c
@@ -3302,6 +3302,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)) {
@@ -3330,6 +3334,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)) {
@@ -3343,9 +3350,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;
}