Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.3k
Description
Functions likePyErr_SetFromErrno() rely on global variableerrno (actually it is thread local, but it does not matter here). They should be called immediately after using a functions which seterrno. Calling other function (likeclose()) can change the value oferrno.Py_DECREF() andPyBuffer_Release() can execute arbitrary code, in particularly the code which changes the value oferrno. EvenPyMem_Free() is not safe, because it the memory allocator can be customized.
There is the same issue withSetFromWindowsErr() and friends. If pass 0 as Windows error code, it callsGetLastError() to retrieve the global value which can be changed at that time if some functions were called beforeSetFromWindowsErr().
Most uses in the code are correct, but there are several sites in the code where some cleanup code is inserted between function which sets the error code and function which consumes it.
Two ways to resolve this issue:
Reorganize the code so that
PyErr_SetFromErrno()andSetFromWindowsErr()are called immediately after function which sets the error code (not counting simple memory reads or writes). In some cases it may require duplicating the cleanup code (usually just one line).Save the error code to a local variable before executing the intermediate code and restore it after.