Repository navigation
gh-162273: Acquire strong reference to fillvalue upfront in itertools.zip_longest - #162274
Closed
espressolee wants to merge 1 commit into
Closed
espressolee wants to merge 1 commit into
espressolee wants to merge 1 commit into
Conversation
…rtools.zip_longest zip_longest_new() borrowed fillvalue from kwds via PyDict_GetItemWithError() and only promoted it with Py_NewRef() after calling PyObject_GetIter() on input iterables. Use PyDict_GetItemRef() upfront and decref fillvalue on error paths so clearing or mutating kwds during iterator initialization cannot free fillvalue before ownership is stored on the zip_longest object.
Contributor
|
The issue is addressed in #156384 which is a more fundamental solution (the |
Contributor
Author
|
Thanks @eendebakpt! I built the branch from #156384 (tested at commit 62f89d3) and ran the reentrancy reproducer against it. The reproducer passes cleanly without crashes, with no local changes to The kwargs-copying change in the If useful, please feel free to reuse |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
zip_longest_new()inModules/itertoolsmodule.cpreviously obtainedfillvaluefromkwdsas a borrowed reference viaPyDict_GetItemWithError()and kept the raw pointer on the C stack acrossPyTuple_New(),PyObject_GetIter()on each input iterable, andtype->tp_alloc()before callingPy_NewRef(fillvalue).If
kwdsis cleared or mutated whilePyObject_GetIter()runs (for example whenzip_longestis called through aPyObject_Call()path that forwardskwargswithout copying, such as_thread.start_new_thread(), or under concurrent mutation),fillvaluecan be deallocated beforelz->fillvalue = Py_NewRef(fillvalue)is reached.Acquire a strong reference upfront using
PyDict_GetItemRef()(orPy_NewRef(Py_None)when no keyword argument is provided), release it withPy_DECREF(fillvalue)on error paths, and transfer ownership directly intolz->fillvalue.