[php-src] PR #22310: Fix use-after-free when ArrayObject sort comparator replaces backing store
| From: | iliaal | Date: | Sun, 14 Jun 2026 17:19:32 +0000 |
| Subject: | [php-src] PR #22310: Fix use-after-free when ArrayObject sort comparator replaces backing store | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-36425@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/22310
Author: iliaal
spl_array_method() caches the backing HashTable pointer across the user-supplied sort comparator. A
comparator that re-enters __construct() (or __unserialize()) routes through spl_array_set_array(),
which unlike the other ArrayObject mutators had no nApplyCount guard, so it swaps intern->array
out from under the cached pointer and the post-sort cleanup releases then dereferences freed memory.
Reachable from pure userland; valgrind reports an invalid read and the process segfaults. Add the
nApplyCount guard the dimension writers and exchangeArray() already use.
```php
$ao = new ArrayObject([3, 1, 2]);
$o = new ArrayObject([9, 8, 7]);
$ao->uasort(function ($a, $b) use ($ao, $o) {
static $n = 0;
if ($n++ === 0) { $ao->__construct($o); }
return $a <=> $b;
});
var_dump($ao->getArrayCopy()); // segfaults before the patch
```