Skip to content

Fix GH-23332: HashTable iterator counter loses its saturation - #23401

Open
iliaal wants to merge 1 commit into
php:PHP-8.4from
iliaal:fix/gh-23332-iterator-count-overflow
Open

Fix GH-23332: HashTable iterator counter loses its saturation#23401
iliaal wants to merge 1 commit into
php:PHP-8.4from
iliaal:fix/gh-23332-iterator-count-overflow

Conversation

@iliaal

@iliaal iliaal commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

zend_hash_iterator_pos_ex() decrements iter->ht's iterator counter while gating that decrement on ht's overflow state. A saturated counter can therefore drop below 0xff and later reach zero with iterators still live, so zend_array_destroy() frees the table without poisoning them. Wrong since ebf900a.

Generators reach the same code as ArrayObject, so the test uses them and lives in Zend/tests. It is red only on a debug build; on a release build valgrind reports the invalid read and write.

luizmenos found the same one-line change independently in #23399 and closed it in favor of this one; NEWS credits both of us.

Fixes GH-23332

nIteratorsCount saturates at 0xff and must stay frozen once it does, because
HT_HAS_ITERATORS() is what makes zend_array_destroy() poison stale iterators
before the table is freed. zend_hash_iterator_pos_ex() decremented the old
table's counter while gating that decrement on the new table's overflow state,
so a saturated counter could drop back below 0xff, later reach zero with
iterators still pointing at the table, and leave it freed unpoisoned. The seven
other arithmetic sites already gate on the table they mutate, and the wholesale
transfers in array_splice() and array_unshift() move the counter intact.

Fixes phpGH-23332
@iliaal
iliaal force-pushed the fix/gh-23332-iterator-count-overflow branch from 16de297 to e3bd6c4 Compare August 21, 2026 14:02

@devnexen devnexen 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.

the fix looks good I may suggest however to wait gina's review before merging

@Girgias
Girgias requested a review from iluuu1994 August 21, 2026 17:09
@Girgias

Girgias commented Aug 21, 2026

Copy link
Copy Markdown
Member

I have never really dealt with the iterator code, and @iluuu1994 seems to be one of the people most recently touching this code so they might have a better opinion.

@LamentXU123 LamentXU123 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.

As an outsider.

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.

4 participants