[php-src] Issue #25378: WeakMap: write/reference fetch with a temporary object key returns a dangling slot (UNKNOWN:0, leaks)

From: Date: Tue, 10 Nov 2026 22:24:10 +0000
Subject: [php-src] Issue #25378: WeakMap: write/reference fetch with a temporary object key returns a dangling slot (UNKNOWN:0, leaks)
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-254115@lists.php.net to get a copy of this message
Issue: https://github.com/php/php-src/issues/25378 Author: jorgsowa ### Description Since GH-20097 (aaed3ccb479), zend_weakmap_read_dimension() inserts a missing key for BP_VAR_W/BP_VAR_RW. With a temporary key, the FETCH_DIM_W handler then frees the TMP key, the weakref notification removes the just-inserted entry, and the returned INDIRECT points at a dead bucket. If the key's destructor frees the WeakMap, the slot points into freed memory. The following code: ```php <?php $wm = new WeakMap; $r = &$wm[new stdClass]; var_dump($r); $wm[new stdClass][] = 1; var_dump(count($wm)); ``` Resulted in this output (debug build): ``` UNKNOWN:0 int(0) Zend/zend_hash.c(159) : Freeing 0x0000000105e02780 (136 bytes), script=t.php Zend/zend_hash.c(352) : Freeing 0x0000000105e06ae0 (56 bytes), script=t.php Zend/zend_execute.c(589) : Freeing 0x0000000105e5d700 (32 bytes), script=t.php === Total 3 memory leaks detected === ``` But I expected this output instead (either an error, as in PHP 8.5.12, or a write into a discarded entry without reading freed memory): ``` Fatal error: Uncaught Error: Object stdClass#2 not contained in WeakMap in t.php:3 ``` Variant where the key's destructor frees the map, which crashes (exit code 139) under guard malloc (USE_ZEND_ALLOC=0): ```php <?php class K { function __destruct() { $GLOBALS['wm'] = null; } } $wm = new WeakMap; $wm[new K][] = str_repeat("x", 10); ``` The underlying ordering issue also exists in 8.5 when the entry already exists and the temporary holds the last reference to the key, which prints UNKNOWN:0 on 8.5.12 as well: ```php <?php $wm = new WeakMap; $k = new stdClass; $wm[$k] = 1; $a = [$k]; unset($k); $r = &$wm[array_pop($a)]; var_dump($r); ``` 8.6 makes it reachable with a plain new X key that 8.5 rejected. Reproduces with opcache off, on, and with tracing JIT. ### PHP Version ```plain PHP 8.6.1-dev (cli) (NTS DEBUG), PHP-8.6 @ dfc3219f341 ``` ### Operating System macOS (arm64)

« previous php.bugs (#254115) next »