-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
_PyGen_FetchStopIterationValue() crashes on unnormalised exceptions #68184
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
Comments
The yield-from implementation calls _PyGen_FetchStopIterationValue() to get the exception value. If the StopIteration exception is not normalised, e.g. because it was set by PyErr_SetObject() in a C extension, then _PyGen_FetchStopIterationValue() will cast to (PyStopIterationObject*) whatever the exception value is and happily interpret an arbitrary memory position as PyObject*. I attached a possible patch for the function. Another place to fix it would be in the yield-from code in ceval.c, but directly genobject.c seems the safer place. |
Here's a better patch that avoids exception normalisation in all "normal" cases. |
And another patch update that should avoid any potential performance regressions due to the additional type check. |
And in fact, fixing it in ceval.c would not be enough, since gen_throw() also calls the function. So this is really the right place to fix it. |
New changeset 15c80f63ea1c by Antoine Pitrou in branch '3.4': New changeset 9d0c6c66b0ac by Antoine Pitrou in branch 'default': |
Thanks for the patch! |
I noticed that my patch isn't entirely correct. If the exception value is a tuple, both PyErr_SetObject() and PyErr_NormalizeException() use it directly as *argument tuple* for the exception instantiation call, i.e. they essentially unpack it into separate arguments. The StopIteration value is then only the first item of that tuple. I wonder if it's worth repeating this, uhm, surprising special case in yet another place, or if we should just always instantiate the exception. |
Here are two patches that fix this case, one with special casing, one without. Please choose and apply one. |
Have you tried benchmarking the "slow" solution? |
No. It's more that it feels wrong to spend actual time on the second most common case that can occur instead of just handling it in no time at all. The third case that it's really required to instantiate the StopIteration exception (if user code didn't do so already, see case 1) should almost never occur in practice. |
The fix wasn't applied yet, so the current code in 3.4 and later branches is still incorrect. Any of the last two patches ("*_value") will fix it, with my preference on the last one. |
Please try to make sure this is fixed before 3.5 rc 1. |
Stefan, the last patch looks good to me. Do you think we can have a unittest for this? |
Could you provide tests covering all branches (normalized exception, unnormalized exception, absent value, non-tuple value, empty tuple value, non-empty tuple value...) Stefan? |
Regarding tests, it looks like iteration isn't currently tested at the C |
Is it possible to test from Python level? |
Looks like I forgot about this. My final fix still hasn't been applied, so the code in Py3.4+ is incorrect now. No, this cannot be tested from the Python level. |
Left a question in code review |
Here is a test that passed with current code but will fail with the patch. I don't know whether it make much sense. If yes, then perhaps aiter_wrapper_iternext needs the same workaround as other invocations of PyErr_SetObject(PyExc_StopIteration, ...). |
Serhiy, I think you forgot to attach the patch. aiter_wrapper shouldn't ever receive tuples, so it should be fine with PyErr_SetObject. |
Stefan, could you please upload a C program that showcases the bug you're trying to fix? |
Yet one special case -- if asynchronous iterator in aiter_wrapper is an instance of StopIteration. Proposed patch adds the function _PyGen_SetStopIterationValue() that raises StopIteration with correctly wrapped value (exception is normalized only if needed) and replaces 4 code duplications with it. The patch also includes Yury's variant of Stefan's patch and additional tests. |
Added comments. |
New changeset bce18f5c0bc4 by Serhiy Storchaka in branch '3.5': New changeset a2c9f06ada28 by Serhiy Storchaka in branch '3.6': New changeset d33b9fd46cef by Serhiy Storchaka in branch 'default': |
I think that's all with this issue. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: