Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-101765: Fix SystemError / segmentation fault in iter __reduce__ when internal access of builtins.__dict__ exhausts the iterator#101769
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
f256afe6b4faada6d62114ccf427c2c9cfbe2989d971960a8efa0540c5abb1445522c64f5fc19049a8ddef4f9557d4afb08e4418dd8ced8e178b8ea49ba8c393854e198ec3c6e66149519ab9c69b664c2c67b11aFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Fix SystemError / segmentation fault in iter ``__reduce__`` when internal access of ``builtins.__dict__`` keys mutates the iter object. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -3444,18 +3444,22 @@ listiter_reduce_general(void *_it, int forward) | ||
| { | ||
| PyObject *list; | ||
| /* _PyEval_GetBuiltin can invoke arbitrary code, | ||
| * call must be before access of iterator pointers. | ||
| * see issue #101765 */ | ||
| /* the objects are not the same, index is of different types! */ | ||
| if (forward){ | ||
| PyObject *iter = _PyEval_GetBuiltin(&_Py_ID(iter)); | ||
| _PyListIterObject *it = (_PyListIterObject *)_it; | ||
| if (it->it_seq){ | ||
| return Py_BuildValue("N(O)n", _PyEval_GetBuiltin(&_Py_ID(iter)), | ||
| it->it_seq, it->it_index); | ||
| return Py_BuildValue("N(O)n", iter, it->it_seq, it->it_index); | ||
| } | ||
| } else{ | ||
| PyObject *reversed = _PyEval_GetBuiltin(&_Py_ID(reversed)); | ||
ionite34 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| listreviterobject *it = (listreviterobject *)_it; | ||
| if (it->it_seq){ | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, but doesn't that mean the old code also leaked references? I'll prepare a PR to fix this now. Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh wait, the N code to Py_BuildValue steals a reference, so the previous code was right in terms of refcounting. | ||
| return Py_BuildValue("N(O)n", _PyEval_GetBuiltin(&_Py_ID(reversed)), | ||
| it->it_seq, it->it_index); | ||
| return Py_BuildValue("N(O)n", reversed, it->it_seq, it->it_index); | ||
| } | ||
| } | ||
| /* empty iterator, create an empty list */ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -14784,14 +14784,19 @@ PyDoc_STRVAR(length_hint_doc, "Private method returning an estimate of len(list( | ||
| static PyObject * | ||
| unicodeiter_reduce(unicodeiterobject *it, PyObject *Py_UNUSED(ignored)) | ||
| { | ||
| PyObject *iter = _PyEval_GetBuiltin(&_Py_ID(iter)); | ||
ionite34 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading. Please reload this page. | ||
| /* _PyEval_GetBuiltin can invoke arbitrary code, | ||
| * call must be before access of iterator pointers. | ||
| * see issue #101765 */ | ||
| if (it->it_seq != NULL){ | ||
| return Py_BuildValue("N(O)n", _PyEval_GetBuiltin(&_Py_ID(iter)), | ||
| it->it_seq, it->it_index); | ||
| return Py_BuildValue("N(O)n", iter, it->it_seq, it->it_index); | ||
| } else{ | ||
| PyObject *u = unicode_new_empty(); | ||
| if (u == NULL) | ||
| return NULL; | ||
| return Py_BuildValue("N(N)", _PyEval_GetBuiltin(&_Py_ID(iter)), u); | ||
| return Py_BuildValue("N(N)", iter, u); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need to del first? We set the correct key on the next line anyway.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's to not invoke the
__eq__from our testCustomStr, since the string key will collide with the same hash. I added some more comments regarding this in c67b11aThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, right! This thing is breaking my assumptions about how dicts should behave :)