[php-src] PR #25111: Fix GH-25107: Preserve foreach iterators on array_unshift
| From: | JoaoMarcos44 | Date: | Sat, 31 Oct 2026 18:49:03 +0000 |
| Subject: | [php-src] PR #25111: Fix GH-25107: Preserve foreach iterators on array_unshift | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-40157@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/25111
Author: JoaoMarcos44
Fixes #25107.
> AI-assisted contribution. The patch, regression test, and explanation were prepared with AI
> assistance and checked with local builds and tests.
## Summary
> Preserve foreach-by-reference iterator positions when
>
array_unshift() compacts holes left by deleted entries. Previously,
> the iterator offset could skip an unvisited value because it still used the old slot position.
> Remap iterator positions to the compacted live slots before adding the prepend offset. Arrays
> without holes keep the existing path. The PHPT covers packed and mixed arrays, dense controls,
> simultaneous iterators, zero and multiple prepends, and end positions. A NEWS entry records the fix.
> The sparse-array path adds a slot scan when active iterators exist. Performance with many
> simultaneous iterators was not measured. Related PR #24475 retains the affected fallback for arrays
> with holes or active iterators.
## Validation
> Target: PHP-8.4, the lowest actively supported affected branch.
> Built with --enable-debug --enable-zts.
> - The new regression fails before the fix and passes afterward.
> - make -j1 passes.
> - make test TESTS='-j1 -q --no-color --no-progress --set-timeout 120
> --show-diff ext/standard/tests/array Zend/tests/*foreach*.phpt' passes with 876
> passed and 13 skipped. No failures or warnings. The runner confirmed that the new regression and
> foreach_016.phpt executed.
> - The full PHP suite and Windows builds were not run.
<details>
<summary>Illustrated explanation</summary>
> AI-generated conceptual illustration. The issue contains the executable reproduction.

[Explanation and
prompt](https://github.com/JoaoMarcos44/php-src/blob/9e60fedb55c1c3bdfaeb339f1beeae2895526596/assets/array/infographic_report.md)
</details>