[3.12] gh-107913: Fix possible losses of OSError error codes (GH-107930) (#108523)

gh-107913: Fix possible losses of OSError error codes (GH-107930)

Functions like PyErr_SetFromErrno() and SetFromWindowsErr() should be
called immediately after using the C API which sets errno or the Windows
error code.
(cherry picked from commit 2b15536fa9)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
Miss Islington (bot) 2023-08-26 16:24:40 -07:00 committed by GitHub
parent bbdd8895a5
commit 3e20303717
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 129 additions and 75 deletions

View file

@ -0,0 +1,3 @@
Fix possible losses of ``errno`` and ``winerror`` values in :exc:`OSError`
exceptions if they were cleared or modified by the cleanup code before
creating the exception object.

View file

@ -3071,8 +3071,8 @@ _curses_getwin(PyObject *module, PyObject *file)
} }
datalen = PyBytes_GET_SIZE(data); datalen = PyBytes_GET_SIZE(data);
if (fwrite(PyBytes_AS_STRING(data), 1, datalen, fp) != datalen) { if (fwrite(PyBytes_AS_STRING(data), 1, datalen, fp) != datalen) {
Py_DECREF(data);
PyErr_SetFromErrno(PyExc_OSError); PyErr_SetFromErrno(PyExc_OSError);
Py_DECREF(data);
goto error; goto error;
} }
Py_DECREF(data); Py_DECREF(data);

View file

@ -394,6 +394,11 @@ _io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode,
if (async_err) if (async_err)
goto error; goto error;
if (self->fd < 0) {
PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, nameobj);
goto error;
}
} }
else { else {
PyObject *fdobj; PyObject *fdobj;
@ -425,12 +430,7 @@ _io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode,
goto error; goto error;
} }
} }
fd_is_own = 1; fd_is_own = 1;
if (self->fd < 0) {
PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, nameobj);
goto error;
}
#ifndef MS_WINDOWS #ifndef MS_WINDOWS
if (_Py_set_inheritable(self->fd, 0, atomic_flag_works) < 0) if (_Py_set_inheritable(self->fd, 0, atomic_flag_works) < 0)
@ -1058,8 +1058,8 @@ _io_FileIO_truncate_impl(fileio *self, PyTypeObject *cls, PyObject *posobj)
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
if (ret != 0) { if (ret != 0) {
Py_DECREF(posobj);
PyErr_SetFromErrno(PyExc_OSError); PyErr_SetFromErrno(PyExc_OSError);
Py_DECREF(posobj);
return NULL; return NULL;
} }

View file

@ -378,8 +378,8 @@ _io__WindowsConsoleIO___init___impl(winconsoleio *self, PyObject *nameobj,
else else
self->fd = _Py_open_osfhandle_noraise(handle, _O_RDONLY | _O_BINARY); self->fd = _Py_open_osfhandle_noraise(handle, _O_RDONLY | _O_BINARY);
if (self->fd < 0) { if (self->fd < 0) {
CloseHandle(handle);
PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, nameobj); PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, nameobj);
CloseHandle(handle);
goto error; goto error;
} }
} }

View file

@ -737,8 +737,8 @@ _locale_bindtextdomain_impl(PyObject *module, const char *domain,
} }
current_dirname = bindtextdomain(domain, dirname); current_dirname = bindtextdomain(domain, dirname);
if (current_dirname == NULL) { if (current_dirname == NULL) {
Py_XDECREF(dirname_bytes);
PyErr_SetFromErrno(PyExc_OSError); PyErr_SetFromErrno(PyExc_OSError);
Py_XDECREF(dirname_bytes);
return NULL; return NULL;
} }
result = PyUnicode_DecodeLocale(current_dirname, NULL); result = PyUnicode_DecodeLocale(current_dirname, NULL);

View file

@ -516,12 +516,12 @@ _multiprocessing_SemLock_impl(PyTypeObject *type, int kind, int value,
return result; return result;
failure: failure:
if (handle != SEM_FAILED)
SEM_CLOSE(handle);
PyMem_Free(name_copy);
if (!PyErr_Occurred()) { if (!PyErr_Occurred()) {
_PyMp_SetError(NULL, MP_STANDARD_ERROR); _PyMp_SetError(NULL, MP_STANDARD_ERROR);
} }
if (handle != SEM_FAILED)
SEM_CLOSE(handle);
PyMem_Free(name_copy);
return NULL; return NULL;
} }
@ -556,8 +556,9 @@ _multiprocessing_SemLock__rebuild_impl(PyTypeObject *type, SEM_HANDLE handle,
if (name != NULL) { if (name != NULL) {
handle = sem_open(name, 0); handle = sem_open(name, 0);
if (handle == SEM_FAILED) { if (handle == SEM_FAILED) {
PyErr_SetFromErrno(PyExc_OSError);
PyMem_Free(name_copy); PyMem_Free(name_copy);
return PyErr_SetFromErrno(PyExc_OSError); return NULL;
} }
} }
#endif #endif

View file

@ -3889,8 +3889,8 @@ _ssl__SSLContext_load_cert_chain_impl(PySSLContext *self, PyObject *certfile,
/* the password callback has already set the error information */ /* the password callback has already set the error information */
} }
else if (errno != 0) { else if (errno != 0) {
ERR_clear_error();
PyErr_SetFromErrno(PyExc_OSError); PyErr_SetFromErrno(PyExc_OSError);
ERR_clear_error();
} }
else { else {
_setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__); _setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__);
@ -3910,8 +3910,8 @@ _ssl__SSLContext_load_cert_chain_impl(PySSLContext *self, PyObject *certfile,
/* the password callback has already set the error information */ /* the password callback has already set the error information */
} }
else if (errno != 0) { else if (errno != 0) {
ERR_clear_error();
PyErr_SetFromErrno(PyExc_OSError); PyErr_SetFromErrno(PyExc_OSError);
ERR_clear_error();
} }
else { else {
_setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__); _setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__);
@ -4140,8 +4140,8 @@ _ssl__SSLContext_load_verify_locations_impl(PySSLContext *self,
PySSL_END_ALLOW_THREADS PySSL_END_ALLOW_THREADS
if (r != 1) { if (r != 1) {
if (errno != 0) { if (errno != 0) {
ERR_clear_error();
PyErr_SetFromErrno(PyExc_OSError); PyErr_SetFromErrno(PyExc_OSError);
ERR_clear_error();
} }
else { else {
_setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__); _setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__);
@ -4188,8 +4188,8 @@ _ssl__SSLContext_load_dh_params(PySSLContext *self, PyObject *filepath)
PySSL_END_ALLOW_THREADS PySSL_END_ALLOW_THREADS
if (dh == NULL) { if (dh == NULL) {
if (errno != 0) { if (errno != 0) {
ERR_clear_error();
PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, filepath); PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, filepath);
ERR_clear_error();
} }
else { else {
_setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__); _setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__);

View file

@ -414,11 +414,10 @@ faulthandler_allocate_stack(void)
int err = sigaltstack(&stack, &old_stack); int err = sigaltstack(&stack, &old_stack);
if (err) { if (err) {
PyErr_SetFromErrno(PyExc_OSError);
/* Release the stack to retry sigaltstack() next time */ /* Release the stack to retry sigaltstack() next time */
PyMem_Free(stack.ss_sp); PyMem_Free(stack.ss_sp);
stack.ss_sp = NULL; stack.ss_sp = NULL;
PyErr_SetFromErrno(PyExc_OSError);
return -1; return -1;
} }
return 0; return 0;

View file

@ -211,11 +211,12 @@ fcntl_ioctl_impl(PyObject *module, int fd, unsigned int code,
if (mutate_arg && (len <= IOCTL_BUFSZ)) { if (mutate_arg && (len <= IOCTL_BUFSZ)) {
memcpy(str, buf, len); memcpy(str, buf, len);
} }
PyBuffer_Release(&pstr); /* No further access to str below this point */
if (ret < 0) { if (ret < 0) {
PyErr_SetFromErrno(PyExc_OSError); PyErr_SetFromErrno(PyExc_OSError);
PyBuffer_Release(&pstr);
return NULL; return NULL;
} }
PyBuffer_Release(&pstr);
if (mutate_arg) { if (mutate_arg) {
return PyLong_FromLong(ret); return PyLong_FromLong(ret);
} }
@ -240,8 +241,8 @@ fcntl_ioctl_impl(PyObject *module, int fd, unsigned int code,
ret = ioctl(fd, code, buf); ret = ioctl(fd, code, buf);
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
if (ret < 0) { if (ret < 0) {
PyBuffer_Release(&pstr);
PyErr_SetFromErrno(PyExc_OSError); PyErr_SetFromErrno(PyExc_OSError);
PyBuffer_Release(&pstr);
return NULL; return NULL;
} }
PyBuffer_Release(&pstr); PyBuffer_Release(&pstr);

View file

@ -341,11 +341,12 @@ getpath_readlines(PyObject *Py_UNUSED(self), PyObject *args)
return NULL; return NULL;
} }
FILE *fp = _Py_wfopen(path, L"rb"); FILE *fp = _Py_wfopen(path, L"rb");
PyMem_Free((void *)path);
if (!fp) { if (!fp) {
PyErr_SetFromErrno(PyExc_OSError); PyErr_SetFromErrno(PyExc_OSError);
PyMem_Free((void *)path);
return NULL; return NULL;
} }
PyMem_Free((void *)path);
r = PyList_New(0); r = PyList_New(0);
if (!r) { if (!r) {

View file

@ -1356,6 +1356,7 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
m_obj->data = mmap(NULL, map_size, prot, flags, fd, offset); m_obj->data = mmap(NULL, map_size, prot, flags, fd, offset);
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
int saved_errno = errno;
if (devzero != -1) { if (devzero != -1) {
close(devzero); close(devzero);
} }
@ -1363,6 +1364,7 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
if (m_obj->data == (char *)-1) { if (m_obj->data == (char *)-1) {
m_obj->data = NULL; m_obj->data = NULL;
Py_DECREF(m_obj); Py_DECREF(m_obj);
errno = saved_errno;
PyErr_SetFromErrno(PyExc_OSError); PyErr_SetFromErrno(PyExc_OSError);
return NULL; return NULL;
} }

View file

@ -367,8 +367,9 @@ _overlapped_RegisterWaitWithQueue_impl(PyObject *module, HANDLE Object,
&NewWaitObject, Object, PostToQueueCallback, pdata, Milliseconds, &NewWaitObject, Object, PostToQueueCallback, pdata, Milliseconds,
WT_EXECUTEINWAITTHREAD | WT_EXECUTEONLYONCE)) WT_EXECUTEINWAITTHREAD | WT_EXECUTEONLYONCE))
{ {
SetFromWindowsErr(0);
PyMem_RawFree(pdata); PyMem_RawFree(pdata);
return SetFromWindowsErr(0); return NULL;
} }
return Py_BuildValue(F_HANDLE, NewWaitObject); return Py_BuildValue(F_HANDLE, NewWaitObject);

View file

@ -3877,9 +3877,10 @@ posix_getcwd(int use_bytes)
return NULL; return NULL;
} }
if (!len) { if (!len) {
PyErr_SetFromWindowsErr(0);
if (wbuf2 != wbuf) if (wbuf2 != wbuf)
PyMem_RawFree(wbuf2); PyMem_RawFree(wbuf2);
return PyErr_SetFromWindowsErr(0); return NULL;
} }
PyObject *resobj = PyUnicode_FromWideChar(wbuf2, len); PyObject *resobj = PyUnicode_FromWideChar(wbuf2, len);
@ -3927,8 +3928,9 @@ posix_getcwd(int use_bytes)
return PyErr_NoMemory(); return PyErr_NoMemory();
} }
if (cwd == NULL) { if (cwd == NULL) {
posix_error();
PyMem_RawFree(buf); PyMem_RawFree(buf);
return posix_error(); return NULL;
} }
PyObject *obj; PyObject *obj;
@ -4140,8 +4142,8 @@ _listdir_windows_no_opendir(path_t *path, PyObject *list)
int error = GetLastError(); int error = GetLastError();
if (error == ERROR_FILE_NOT_FOUND) if (error == ERROR_FILE_NOT_FOUND)
goto exit; goto exit;
Py_DECREF(list); path_error(path);
list = path_error(path); Py_CLEAR(list);
goto exit; goto exit;
} }
do { do {
@ -4154,12 +4156,12 @@ _listdir_windows_no_opendir(path_t *path, PyObject *list)
Py_SETREF(v, PyUnicode_EncodeFSDefault(v)); Py_SETREF(v, PyUnicode_EncodeFSDefault(v));
} }
if (v == NULL) { if (v == NULL) {
Py_SETREF(list, NULL); Py_CLEAR(list);
break; break;
} }
if (PyList_Append(list, v) != 0) { if (PyList_Append(list, v) != 0) {
Py_DECREF(v); Py_DECREF(v);
Py_SETREF(list, NULL); Py_CLEAR(list);
break; break;
} }
Py_DECREF(v); Py_DECREF(v);
@ -4170,8 +4172,8 @@ _listdir_windows_no_opendir(path_t *path, PyObject *list)
/* FindNextFile sets error to ERROR_NO_MORE_FILES if /* FindNextFile sets error to ERROR_NO_MORE_FILES if
it got to the end of the directory. */ it got to the end of the directory. */
if (!result && GetLastError() != ERROR_NO_MORE_FILES) { if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
Py_DECREF(list); path_error(path);
list = path_error(path); Py_CLEAR(list);
goto exit; goto exit;
} }
} while (result == TRUE); } while (result == TRUE);
@ -4180,8 +4182,8 @@ exit:
if (hFindFile != INVALID_HANDLE_VALUE) { if (hFindFile != INVALID_HANDLE_VALUE) {
if (FindClose(hFindFile) == FALSE) { if (FindClose(hFindFile) == FALSE) {
if (list != NULL) { if (list != NULL) {
Py_DECREF(list); path_error(path);
list = path_error(path); Py_CLEAR(list);
} }
} }
} }
@ -4243,7 +4245,8 @@ _posix_listdir(path_t *path, PyObject *list)
} }
if (dirp == NULL) { if (dirp == NULL) {
list = path_error(path); path_error(path);
list = NULL;
#ifdef HAVE_FDOPENDIR #ifdef HAVE_FDOPENDIR
if (fd != -1) { if (fd != -1) {
Py_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
@ -4265,8 +4268,8 @@ _posix_listdir(path_t *path, PyObject *list)
if (errno == 0) { if (errno == 0) {
break; break;
} else { } else {
Py_DECREF(list); path_error(path);
list = path_error(path); Py_CLEAR(list);
goto exit; goto exit;
} }
} }
@ -6609,8 +6612,9 @@ os_execv_impl(PyObject *module, path_t *path, PyObject *argv)
/* If we get here it's definitely an error */ /* If we get here it's definitely an error */
posix_error();
free_string_array(argvlist, argc); free_string_array(argvlist, argc);
return posix_error(); return NULL;
} }
@ -6914,11 +6918,12 @@ parse_file_actions(PyObject *file_actions,
} }
errno = posix_spawn_file_actions_addopen(file_actionsp, errno = posix_spawn_file_actions_addopen(file_actionsp,
fd, PyBytes_AS_STRING(path), oflag, (mode_t)mode); fd, PyBytes_AS_STRING(path), oflag, (mode_t)mode);
Py_DECREF(path);
if (errno) { if (errno) {
posix_error(); posix_error();
Py_DECREF(path);
goto fail; goto fail;
} }
Py_DECREF(path);
break; break;
} }
case POSIX_SPAWN_CLOSE: { case POSIX_SPAWN_CLOSE: {
@ -7315,12 +7320,15 @@ os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv)
_Py_END_SUPPRESS_IPH _Py_END_SUPPRESS_IPH
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
int saved_errno = errno;
free_string_array(argvlist, argc); free_string_array(argvlist, argc);
if (spawnval == -1) if (spawnval == -1) {
return posix_error(); errno = saved_errno;
else posix_error();
return Py_BuildValue(_Py_PARSE_INTPTR, spawnval); return NULL;
}
return Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
} }
/*[clinic input] /*[clinic input]
@ -7638,6 +7646,7 @@ os_fork1_impl(PyObject *module)
} }
PyOS_BeforeFork(); PyOS_BeforeFork();
pid = fork1(); pid = fork1();
int saved_errno = errno;
if (pid == 0) { if (pid == 0) {
/* child: this clobbers and resets the import lock. */ /* child: this clobbers and resets the import lock. */
PyOS_AfterFork_Child(); PyOS_AfterFork_Child();
@ -7646,8 +7655,10 @@ os_fork1_impl(PyObject *module)
/* parent: release the import lock. */ /* parent: release the import lock. */
PyOS_AfterFork_Parent(); PyOS_AfterFork_Parent();
} }
if (pid == -1) if (pid == -1) {
errno = saved_errno;
return posix_error(); return posix_error();
}
return PyLong_FromPid(pid); return PyLong_FromPid(pid);
} }
#endif /* HAVE_FORK1 */ #endif /* HAVE_FORK1 */
@ -7683,6 +7694,7 @@ os_fork_impl(PyObject *module)
} }
PyOS_BeforeFork(); PyOS_BeforeFork();
pid = fork(); pid = fork();
int saved_errno = errno;
if (pid == 0) { if (pid == 0) {
/* child: this clobbers and resets the import lock. */ /* child: this clobbers and resets the import lock. */
PyOS_AfterFork_Child(); PyOS_AfterFork_Child();
@ -7691,8 +7703,10 @@ os_fork_impl(PyObject *module)
/* parent: release the import lock. */ /* parent: release the import lock. */
PyOS_AfterFork_Parent(); PyOS_AfterFork_Parent();
} }
if (pid == -1) if (pid == -1) {
errno = saved_errno;
return posix_error(); return posix_error();
}
return PyLong_FromPid(pid); return PyLong_FromPid(pid);
} }
#endif /* HAVE_FORK */ #endif /* HAVE_FORK */
@ -8229,13 +8243,17 @@ os_openpty_impl(PyObject *module)
/* change permission of slave */ /* change permission of slave */
if (grantpt(master_fd) < 0) { if (grantpt(master_fd) < 0) {
int saved_errno = errno;
PyOS_setsig(SIGCHLD, sig_saved); PyOS_setsig(SIGCHLD, sig_saved);
errno = saved_errno;
goto posix_error; goto posix_error;
} }
/* unlock slave */ /* unlock slave */
if (unlockpt(master_fd) < 0) { if (unlockpt(master_fd) < 0) {
int saved_errno = errno;
PyOS_setsig(SIGCHLD, sig_saved); PyOS_setsig(SIGCHLD, sig_saved);
errno = saved_errno;
goto posix_error; goto posix_error;
} }
@ -8599,8 +8617,9 @@ os_getgroups_impl(PyObject *module)
n = getgroups(n, grouplist); n = getgroups(n, grouplist);
if (n == -1) { if (n == -1) {
posix_error();
PyMem_Free(grouplist); PyMem_Free(grouplist);
return posix_error(); return NULL;
} }
PyObject *result = PyList_New(n); PyObject *result = PyList_New(n);
@ -9163,8 +9182,9 @@ os_setgroups(PyObject *module, PyObject *groups)
} }
if (setgroups(len, grouplist) < 0) { if (setgroups(len, grouplist) < 0) {
posix_error();
PyMem_Free(grouplist); PyMem_Free(grouplist);
return posix_error(); return NULL;
} }
PyMem_Free(grouplist); PyMem_Free(grouplist);
Py_RETURN_NONE; Py_RETURN_NONE;
@ -10611,10 +10631,13 @@ os_readv_impl(PyObject *module, int fd, PyObject *buffers)
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
} while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
int saved_errno = errno;
iov_cleanup(iov, buf, cnt); iov_cleanup(iov, buf, cnt);
if (n < 0) { if (n < 0) {
if (!async_err) if (!async_err) {
errno = saved_errno;
posix_error(); posix_error();
}
return -1; return -1;
} }
@ -10663,8 +10686,11 @@ os_pread_impl(PyObject *module, int fd, Py_ssize_t length, Py_off_t offset)
} while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
if (n < 0) { if (n < 0) {
if (!async_err) {
posix_error();
}
Py_DECREF(buffer); Py_DECREF(buffer);
return (!async_err) ? posix_error() : NULL; return NULL;
} }
if (n != length) if (n != length)
_PyBytes_Resize(&buffer, n); _PyBytes_Resize(&buffer, n);
@ -10761,9 +10787,11 @@ os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
#endif #endif
int saved_errno = errno;
iov_cleanup(iov, buf, cnt); iov_cleanup(iov, buf, cnt);
if (n < 0) { if (n < 0) {
if (!async_err) { if (!async_err) {
errno = saved_errno;
posix_error(); posix_error();
} }
return -1; return -1;
@ -10932,24 +10960,26 @@ os_sendfile_impl(PyObject *module, int out_fd, int in_fd, PyObject *offobj,
} while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
_Py_END_SUPPRESS_IPH _Py_END_SUPPRESS_IPH
int saved_errno = errno;
if (sf.headers != NULL) if (sf.headers != NULL)
iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
if (sf.trailers != NULL) if (sf.trailers != NULL)
iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
if (ret < 0) { if (ret < 0) {
if ((errno == EAGAIN) || (errno == EBUSY)) { if ((saved_errno == EAGAIN) || (saved_errno == EBUSY)) {
if (sbytes != 0) { if (sbytes != 0) {
// some data has been sent // some data has been sent
goto done; goto done;
} }
else { // no data has been sent; upper application is supposed
// no data has been sent; upper application is supposed // to retry on EAGAIN or EBUSY
// to retry on EAGAIN or EBUSY
return posix_error();
}
} }
return (!async_err) ? posix_error() : NULL; if (!async_err) {
errno = saved_errno;
posix_error();
}
return NULL;
} }
goto done; goto done;
@ -11266,10 +11296,10 @@ os_writev_impl(PyObject *module, int fd, PyObject *buffers)
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
} while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
iov_cleanup(iov, buf, cnt);
if (result < 0 && !async_err) if (result < 0 && !async_err)
posix_error(); posix_error();
iov_cleanup(iov, buf, cnt);
return result; return result;
} }
#endif /* HAVE_WRITEV */ #endif /* HAVE_WRITEV */
@ -11404,13 +11434,13 @@ os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
#endif #endif
iov_cleanup(iov, buf, cnt);
if (result < 0) { if (result < 0) {
if (!async_err) { if (!async_err) {
posix_error(); posix_error();
} }
return -1; result = -1;
} }
iov_cleanup(iov, buf, cnt);
return result; return result;
} }
@ -11972,12 +12002,13 @@ win32_putenv(PyObject *name, PyObject *value)
Prefer _wputenv() to be compatible with C libraries using CRT Prefer _wputenv() to be compatible with C libraries using CRT
variables and CRT functions using these variables (ex: getenv()). */ variables and CRT functions using these variables (ex: getenv()). */
int err = _wputenv(env); int err = _wputenv(env);
PyMem_Free(env);
if (err) { if (err) {
posix_error(); posix_error();
PyMem_Free(env);
return NULL; return NULL;
} }
PyMem_Free(env);
Py_RETURN_NONE; Py_RETURN_NONE;
} }
@ -13819,10 +13850,12 @@ os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Py_END_ALLOW_THREADS; Py_END_ALLOW_THREADS;
if (result < 0) { if (result < 0) {
Py_DECREF(buffer); if (errno == ERANGE) {
if (errno == ERANGE) Py_DECREF(buffer);
continue; continue;
}
path_error(path); path_error(path);
Py_DECREF(buffer);
return NULL; return NULL;
} }
@ -14589,14 +14622,19 @@ DirEntry_fetch_stat(PyObject *module, DirEntry *self, int follow_symlinks)
} }
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
} }
int saved_errno = errno;
#if defined(MS_WINDOWS) #if defined(MS_WINDOWS)
PyMem_Free(path); PyMem_Free(path);
#else #else
Py_DECREF(ub); Py_DECREF(ub);
#endif #endif
if (result != 0) if (result != 0) {
return path_object_error(self->path); errno = saved_errno;
path_object_error(self->path);
return NULL;
}
return _pystat_fromstructstat(module, &st); return _pystat_fromstructstat(module, &st);
} }
@ -14788,10 +14826,14 @@ os_DirEntry_inode_impl(DirEntry *self)
wchar_t *path = PyUnicode_AsWideCharString(unicode, NULL); wchar_t *path = PyUnicode_AsWideCharString(unicode, NULL);
Py_DECREF(unicode); Py_DECREF(unicode);
result = LSTAT(path, &stat); result = LSTAT(path, &stat);
int saved_errno = errno;
PyMem_Free(path); PyMem_Free(path);
if (result != 0) if (result != 0) {
errno = saved_errno;
return path_object_error(self->path); return path_object_error(self->path);
}
self->win32_file_index = stat.st_ino; self->win32_file_index = stat.st_ino;
self->got_file_index = 1; self->got_file_index = 1;
@ -15355,12 +15397,12 @@ os_scandir_impl(PyObject *module, path_t *path)
iterator->handle = FindFirstFileW(path_strW, &iterator->file_data); iterator->handle = FindFirstFileW(path_strW, &iterator->file_data);
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
PyMem_Free(path_strW);
if (iterator->handle == INVALID_HANDLE_VALUE) { if (iterator->handle == INVALID_HANDLE_VALUE) {
path_error(&iterator->path); path_error(&iterator->path);
PyMem_Free(path_strW);
goto error; goto error;
} }
PyMem_Free(path_strW);
#else /* POSIX */ #else /* POSIX */
errno = 0; errno = 0;
#ifdef HAVE_FDOPENDIR #ifdef HAVE_FDOPENDIR

View file

@ -1292,8 +1292,8 @@ newPyEpoll_Object(PyTypeObject *type, int sizehint, SOCKET fd)
self->epfd = fd; self->epfd = fd;
} }
if (self->epfd < 0) { if (self->epfd < 0) {
Py_DECREF(self);
PyErr_SetFromErrno(PyExc_OSError); PyErr_SetFromErrno(PyExc_OSError);
Py_DECREF(self);
return NULL; return NULL;
} }
@ -1971,8 +1971,8 @@ newKqueue_Object(PyTypeObject *type, SOCKET fd)
self->kqfd = fd; self->kqfd = fd;
} }
if (self->kqfd < 0) { if (self->kqfd < 0) {
Py_DECREF(self);
PyErr_SetFromErrno(PyExc_OSError); PyErr_SetFromErrno(PyExc_OSError);
Py_DECREF(self);
return NULL; return NULL;
} }

View file

@ -5401,8 +5401,8 @@ sock_initobj_impl(PySocketSockObject *self, int family, int type, int proto,
} }
if (!SetHandleInformation((HANDLE)fd, HANDLE_FLAG_INHERIT, 0)) { if (!SetHandleInformation((HANDLE)fd, HANDLE_FLAG_INHERIT, 0)) {
closesocket(fd);
PyErr_SetFromWindowsErr(0); PyErr_SetFromWindowsErr(0);
closesocket(fd);
return -1; return -1;
} }
@ -5616,8 +5616,9 @@ socket_gethostname(PyObject *self, PyObject *unused)
name, name,
&size)) &size))
{ {
PyErr_SetFromWindowsErr(0);
PyMem_Free(name); PyMem_Free(name);
return PyErr_SetFromWindowsErr(0); return NULL;
} }
result = PyUnicode_FromWideChar(name, size); result = PyUnicode_FromWideChar(name, size);
@ -6215,8 +6216,8 @@ socket_dup(PyObject *self, PyObject *fdobj)
} }
if (!SetHandleInformation((HANDLE)newfd, HANDLE_FLAG_INHERIT, 0)) { if (!SetHandleInformation((HANDLE)newfd, HANDLE_FLAG_INHERIT, 0)) {
closesocket(newfd);
PyErr_SetFromWindowsErr(0); PyErr_SetFromWindowsErr(0);
closesocket(newfd);
return NULL; return NULL;
} }
#else #else
@ -6663,11 +6664,12 @@ socket_inet_ntop(PyObject *self, PyObject *args)
/* inet_ntop guarantee NUL-termination of resulting string. */ /* inet_ntop guarantee NUL-termination of resulting string. */
retval = inet_ntop(af, packed_ip.buf, ip, sizeof(ip)); retval = inet_ntop(af, packed_ip.buf, ip, sizeof(ip));
PyBuffer_Release(&packed_ip);
if (!retval) { if (!retval) {
PyErr_SetFromErrno(PyExc_OSError); PyErr_SetFromErrno(PyExc_OSError);
PyBuffer_Release(&packed_ip);
return NULL; return NULL;
} else { } else {
PyBuffer_Release(&packed_ip);
return PyUnicode_FromString(retval); return PyUnicode_FromString(retval);
} }
} }
@ -7006,8 +7008,8 @@ socket_if_nameindex(PyObject *self, PyObject *arg)
ni = if_nameindex(); ni = if_nameindex();
if (ni == NULL) { if (ni == NULL) {
Py_DECREF(list);
PyErr_SetFromErrno(PyExc_OSError); PyErr_SetFromErrno(PyExc_OSError);
Py_DECREF(list);
return NULL; return NULL;
} }

View file

@ -7060,7 +7060,7 @@ decode_code_page_errors(UINT code_page,
if (err != ERROR_NO_UNICODE_TRANSLATION if (err != ERROR_NO_UNICODE_TRANSLATION
&& err != ERROR_INSUFFICIENT_BUFFER) && err != ERROR_INSUFFICIENT_BUFFER)
{ {
PyErr_SetFromWindowsErr(0); PyErr_SetFromWindowsErr(err);
goto error; goto error;
} }
insize++; insize++;

View file

@ -1790,6 +1790,7 @@ _Py_fopen_obj(PyObject *path, const char *mode)
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
} while (f == NULL } while (f == NULL
&& errno == EINTR && !(async_err = PyErr_CheckSignals())); && errno == EINTR && !(async_err = PyErr_CheckSignals()));
int saved_errno = errno;
PyMem_Free(wpath); PyMem_Free(wpath);
#else #else
PyObject *bytes; PyObject *bytes;
@ -1812,13 +1813,14 @@ _Py_fopen_obj(PyObject *path, const char *mode)
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
} while (f == NULL } while (f == NULL
&& errno == EINTR && !(async_err = PyErr_CheckSignals())); && errno == EINTR && !(async_err = PyErr_CheckSignals()));
int saved_errno = errno;
Py_DECREF(bytes); Py_DECREF(bytes);
#endif #endif
if (async_err) if (async_err)
return NULL; return NULL;
if (f == NULL) { if (f == NULL) {
errno = saved_errno;
PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path); PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
return NULL; return NULL;
} }