Skip to content

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

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

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

Conversation

@iliaal

@iliaal iliaal commented Aug 21, 2026

Copy link
Copy Markdown
Owner

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 the table gets freed without poisoning them. Wrong since ebf900a. SPL isn't required to trigger it, so the test uses generators and lives in Zend/tests. It is red only on a debug build; on a release build valgrind shows the invalid read and write.

Fixes php#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 5df5008 to 16de297 Compare August 21, 2026 13:57
@iliaal

iliaal commented Aug 21, 2026

Copy link
Copy Markdown
Owner Author

Promoted upstream: php#23401

@iliaal iliaal closed this Aug 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant