Skip to content

gh-154672: Fix use-after-free in itertools.zip_longest via re-entrant iterator#154673

Open
tonghuaroot wants to merge 5 commits into
python:mainfrom
tonghuaroot:fix-zip-longest-reentrant-uaf
Open

gh-154672: Fix use-after-free in itertools.zip_longest via re-entrant iterator#154673
tonghuaroot wants to merge 5 commits into
python:mainfrom
tonghuaroot:fix-zip-longest-reentrant-uaf

Conversation

@tonghuaroot

@tonghuaroot tonghuaroot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

zip_longest_next_lock_held() borrows iterators from lz->ittuple via PyTuple_GET_ITEM without Py_INCREF, then calls PyIter_Next(it). A re-entrant callback can exhaust and free the iterator while the outer call is still using it.

Fix: Py_INCREF(it) before PyIter_Next, Py_DECREF after. Guard the exhaust handler against double-free when a re-entrant call already removed the iterator.

@brijkapadia

Copy link
Copy Markdown
Contributor

I already have an open PR that would fix this use-after-free (#150675),

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