[php-src] PR #11248: Track HashTableIterators for copy-on-write copies of HashTables
| From: | bwoebi | Date: | Tue, 16 May 2023 01:18:55 +0000 |
| Subject: | [php-src] PR #11248: Track HashTableIterators for copy-on-write copies of HashTables | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-22521@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/11248
Author: bwoebi
When executing a foreach ($ht as &$ref), foreach calls zend_hash_iterator_pos_ex() on every
iteration. If the HashTable contained in the $ht variable is not the tracked HashTable, it will
reset the position to the internal array pointer of the array currently in $ht. This behaviour is
generally fine, but undesirable for copy-on-write copies of the iterated HashTable. This may
trivially occur when the iterated over HashTable is assigned to some variable, then the iterated
over variable modified, leading to array separation, changing the HashTable pointer in the variable.
Thus foreach happily restarting iteration. This behaviour (despite existing since PHP 7.0) is
considered a bug, if not only for the behaviour being unexpected to the user, also copy-on-write
should not have trivially observable side-effects by mere assignment.
The bugfix consists of duplicating HashTableIterators whenever zend_array_dup() is called (the
primitive used on array separation). When a further access to the HashPosition through the
HashTableIterators API happens and the HashTable does not match the tracked one, all the duplicates
(which are tracked by single linked list) are searched for the wanted HashTable. If found, the
HashTableIterator is replaced by the found copy and all other copies are removed. This ensures that
we always end up tracking the correct HashTable.
Note that I'm adding a
uint32_t next_copy to the HashTableIterator struct, which
would break ABI for 32-bit builds, thus targeting master only.
Given that it's longstanding behaviour, it might be okay to only fix this in PHP 8.3.
I have no idea how one would fix that in PHP 8.1. An alternative could be fixing it for 64 bit
targets only...
Fixes GH-11244.