Skip to content

Fix GH-17935: Separate ArrayObject and ArrayIterator storage on write - #25432

Draft
nicolas-grekas wants to merge 1 commit into
php:masterfrom
nicolas-grekas:spl-array-separate-on-write
Draft

nicolas-grekas wants to merge 1 commit into
php:masterfrom
nicolas-grekas:spl-array-separate-on-write

Conversation

@nicolas-grekas

@nicolas-grekas nicolas-grekas commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

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() or json_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.

foreach over an ArrayObject needs about 3% more instructions.

@iliaal iliaal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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";

Comment thread ext/spl/spl_array.c Resolved
Comment thread ext/spl/spl_array.c Resolved
Comment thread ext/spl/spl_array.c Resolved
@nicolas-grekas
nicolas-grekas force-pushed the spl-array-separate-on-write branch from 47c7da9 to 461ffd1 Compare November 15, 2026 09:12
@nicolas-grekas

Copy link
Copy Markdown
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 ksort() change to the description.

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
nicolas-grekas force-pushed the spl-array-separate-on-write branch from 461ffd1 to b509d1b Compare November 15, 2026 16:52
@nicolas-grekas

Copy link
Copy Markdown
Contributor Author

PR updated: I moved the fixes for the error handler and destructor cases to #25469 and #25470. These bugs also exist on 8.4, so they should be fixed there first. That includes your points 2 and 3 @iliaal. This PR is now only about the separation.

@nicolas-grekas
nicolas-grekas marked this pull request as draft November 15, 2026 17:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Assertion failure Zend/zend_hash.c when appending to serialized ArrayObject

2 participants