Skip to content

Fix iterator position resetting #13188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed

Conversation

iluuu1994
Copy link
Member

Previously, when an array was converted from packed to hashed, iterators would not be correctly reset to 0. Similarly, removing the last element from an array would decrease nNumUsed but not actually fix the iterator position, causing the element to be skipped in the next iteration.

Some code was also removed that skips over IS_UNDEF elements for nInternalPointer and iterator positions. This is unnecessary, as this already happens during iteration.

Closes GH-13178

Previously, when an array was converted from packed to hashed, iterators would
not be correctly reset to 0. Similarly, removing the last element from an array
would decrease nNumUsed but not actually fix the iterator position, causing the
element to be skipped in the next iteration.

Some code was also removed that skips over IS_UNDEF elements for
nInternalPointer and iterator positions. This is unnecessary, as this already
happens during iteration.

Closes phpGH-13178
@bwoebi
Copy link
Member

bwoebi commented Jan 18, 2024

The changes to zend_hash.c are fine, but you need to add extra support to php_splice() directly as that function really splices into the middle of the target array by doing a copy of it.

(Also: why does php_splice unconditionally do a copy instead of like ... just in case the replacement array is bigger than the $length arg? .. Right, because it resets numerical keys, for reasons.)

@iluuu1994
Copy link
Member Author

@bwoebi php_slice is adjusted accordingly. Tbh the existing behavior is somewhat dubious though. I would expect the newly appended elements to be seen, not skipped.

@iluuu1994 iluuu1994 closed this in d653646 Jan 21, 2024
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.

Foreach loop with value by reference and using unset and set to array creates overflow memory.
2 participants