Repository navigation
Fix GH-17935: Separate ArrayObject and ArrayIterator storage on write - #25432
Draft
nicolas-grekas wants to merge 1 commit into
Draft
nicolas-grekas wants to merge 1 commit into
nicolas-grekas wants to merge 1 commit into
Conversation
iliaal
reviewed
Nov 13, 2026
iliaal
left a comment
Member
There was a problem hiding this comment.
With the hole before the iterator's position, ksort() now leaves it on a different element: master gives c, this gives b. Probably the more correct one, but worth a line in the description.
$ao = new ArrayObject(['z' => 0, 'a' => 1, 'b' => 2, 'c' => 3]);
unset($ao['z']);
$it = $ao->getIterator();
$it->next();
$ao->ksort();
echo $it->key(), "\n";
nicolas-grekas
force-pushed
the
spl-array-separate-on-write
branch
from
November 15, 2026 09:12
47c7da9 to
461ffd1
Compare
Contributor
Author
|
Thank you @iliaal, comments addressed. When the storage is replaced, iterators now always keep their numeric position, as on master. A copy of the iterator held by the new array is ignored then, using an id that changes on each replacement. The new test also shows that this position still depends on holes in the new array, like on master. I fixed the object-backed case too, since it's the same missing re-fetch. And I added the |
ArrayObject and ArrayIterator assumed they were the only owner of their storage, but __serialize(), __debugInfo(), var_export(), json_encode() and the sort methods hand it out or borrow it without copying it. Writing to the object then modified the array that was handed out, and failed the refcount assertion of zend_hash.c in debug builds. The storage is now separated before any write. When this happens while it is iterated, iterators move to the copy of their position that zend_array_dup() registers on the new table. When the storage is replaced, they keep their position and get registered on the new table. Each replacement gives the storage a new id, so that a copy of the iterator that the new array may hold is ignored then.
nicolas-grekas
force-pushed
the
spl-array-separate-on-write
branch
from
November 15, 2026 16:52
461ffd1 to
b509d1b
Compare
Contributor
Author
nicolas-grekas
marked this pull request as draft
November 15, 2026 17:01
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes GH-17935. Split from #23945.
ArrayObject and ArrayIterator write to their storage in place, even when it's shared with an array returned by
__serialize(),__debugInfo(),var_export()orjson_encode(). This PR separates the storage before any write instead, as discussed in #17942. Iterators move to the copy of their position.When the storage is replaced, iterators keep their numeric position, as on master.
There's one visible change. Sorting no longer moves iterators to another element when the storage has holes.
An error handler or a destructor can change the storage in the middle of an access. That can already be a use-after-free on master, see #25469 and #25470. With this PR, it's also the case when the handler makes the storage separate. I didn't add local guards, since these cases need the global fixes of #20018 and #20001.
foreachover an ArrayObject needs about 3% more instructions.