Skip to content

Zend: Fix iterator relocation at the current element during rehash - #23607

Open
LamentXU123 wants to merge 2 commits into
php:PHP-8.4from
LamentXU123:codex/fix-rehash-iterator-position
Open

Zend: Fix iterator relocation at the current element during rehash#23607
LamentXU123 wants to merge 2 commits into
php:PHP-8.4from
LamentXU123:codex/fix-rehash-iterator-position

Conversation

@LamentXU123

@LamentXU123 LamentXU123 commented Sep 7, 2026

Copy link
Copy Markdown
Member

Found when I am implementing #23585

<?php
$a = [0, 1, 2];
foreach ($a as &$outer) {
    foreach ($a as &$value) {
        echo $value, "\n";
        if ($value === 1) {
            unset($a[0], $a[1]);
            $a['x'] = 3; 
        }
    }
    break;
}

Return

0
1

But I expect:

0
1
2
3

Comment thread Zend/zend_hash.c
zend_hash_iterators_update(ht, iter_pos, j);
iter_pos = zend_hash_iterators_lower_pos(ht, iter_pos + 1);
} while (iter_pos < i);
} while (iter_pos <= i);

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.

nit: while at it, you can also fix zend_array_dup_elements()

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Right. and I think this is the similar fix to the existing one so we don't need to add more NEWS entry.

@LamentXU123 LamentXU123 closed this Sep 7, 2026
@LamentXU123
LamentXU123 deleted the codex/fix-rehash-iterator-position branch September 7, 2026 16:21
@LamentXU123
LamentXU123 restored the codex/fix-rehash-iterator-position branch September 7, 2026 16:21
@LamentXU123 LamentXU123 reopened this Sep 7, 2026
When one iterator points at a hole and another at the next live bucket,
the relocation loop must include the iterator at that bucket's original
position. Otherwise it is moved to the following bucket's destination,
causing a nested by-reference foreach to skip an element.

Add regression coverage for packed-to-hash conversion and compaction of
a full mixed table.
@LamentXU123
LamentXU123 force-pushed the codex/fix-rehash-iterator-position branch from cb88dd2 to 2e62b97 Compare September 7, 2026 16:28
@LamentXU123

Copy link
Copy Markdown
Member Author

I am testing my new agent workflow to help me branching written PR to remote and obviously it glitched so this is unintentionally closed and reopened. Sorry for the noise.

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.

2 participants