--- /dev/null
+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.
}
datalen = PyBytes_GET_SIZE(data);
if (fwrite(PyBytes_AS_STRING(data), 1, datalen, fp) != datalen) {
- Py_DECREF(data);
PyErr_SetFromErrno(PyExc_OSError);
+ Py_DECREF(data);
goto error;
}
Py_DECREF(data);
if (async_err)
goto error;
+
+ if (self->fd < 0) {
+ PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, nameobj);
+ goto error;
+ }
}
else {
PyObject *fdobj;
goto error;
}
}
-
fd_is_own = 1;
- if (self->fd < 0) {
- PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, nameobj);
- goto error;
- }
#ifndef MS_WINDOWS
if (_Py_set_inheritable(self->fd, 0, atomic_flag_works) < 0)
Py_END_ALLOW_THREADS
if (ret != 0) {
- Py_DECREF(posobj);
PyErr_SetFromErrno(PyExc_OSError);
+ Py_DECREF(posobj);
return NULL;
}
else
self->fd = _Py_open_osfhandle_noraise(handle, _O_RDONLY | _O_BINARY);
if (self->fd < 0) {
- CloseHandle(handle);
PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, nameobj);
+ CloseHandle(handle);
goto error;
}
}
}
current_dirname = bindtextdomain(domain, dirname);
if (current_dirname == NULL) {
- Py_XDECREF(dirname_bytes);
PyErr_SetFromErrno(PyExc_OSError);
+ Py_XDECREF(dirname_bytes);
return NULL;
}
result = PyUnicode_DecodeLocale(current_dirname, NULL);
return result;
failure:
- if (handle != SEM_FAILED)
- SEM_CLOSE(handle);
- PyMem_Free(name_copy);
if (!PyErr_Occurred()) {
_PyMp_SetError(NULL, MP_STANDARD_ERROR);
}
+ if (handle != SEM_FAILED)
+ SEM_CLOSE(handle);
+ PyMem_Free(name_copy);
return NULL;
}
if (name != NULL) {
handle = sem_open(name, 0);
if (handle == SEM_FAILED) {
+ PyErr_SetFromErrno(PyExc_OSError);
PyMem_Free(name_copy);
- return PyErr_SetFromErrno(PyExc_OSError);
+ return NULL;
}
}
#endif
/* the password callback has already set the error information */
}
else if (errno != 0) {
- ERR_clear_error();
PyErr_SetFromErrno(PyExc_OSError);
+ ERR_clear_error();
}
else {
_setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__);
/* the password callback has already set the error information */
}
else if (errno != 0) {
- ERR_clear_error();
PyErr_SetFromErrno(PyExc_OSError);
+ ERR_clear_error();
}
else {
_setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__);
PySSL_END_ALLOW_THREADS
if (r != 1) {
if (errno != 0) {
- ERR_clear_error();
PyErr_SetFromErrno(PyExc_OSError);
+ ERR_clear_error();
}
else {
_setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__);
PySSL_END_ALLOW_THREADS
if (dh == NULL) {
if (errno != 0) {
- ERR_clear_error();
PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, filepath);
+ ERR_clear_error();
}
else {
_setSSLError(get_state_ctx(self), NULL, 0, __FILE__, __LINE__);
int err = sigaltstack(&stack, &old_stack);
if (err) {
+ PyErr_SetFromErrno(PyExc_OSError);
/* Release the stack to retry sigaltstack() next time */
PyMem_Free(stack.ss_sp);
stack.ss_sp = NULL;
-
- PyErr_SetFromErrno(PyExc_OSError);
return -1;
}
return 0;
if (mutate_arg && (len <= IOCTL_BUFSZ)) {
memcpy(str, buf, len);
}
- PyBuffer_Release(&pstr); /* No further access to str below this point */
if (ret < 0) {
PyErr_SetFromErrno(PyExc_OSError);
+ PyBuffer_Release(&pstr);
return NULL;
}
+ PyBuffer_Release(&pstr);
if (mutate_arg) {
return PyLong_FromLong(ret);
}
ret = ioctl(fd, code, buf);
Py_END_ALLOW_THREADS
if (ret < 0) {
- PyBuffer_Release(&pstr);
PyErr_SetFromErrno(PyExc_OSError);
+ PyBuffer_Release(&pstr);
return NULL;
}
PyBuffer_Release(&pstr);
return NULL;
}
FILE *fp = _Py_wfopen(path, L"rb");
- PyMem_Free((void *)path);
if (!fp) {
PyErr_SetFromErrno(PyExc_OSError);
+ PyMem_Free((void *)path);
return NULL;
}
+ PyMem_Free((void *)path);
r = PyList_New(0);
if (!r) {
m_obj->data = mmap(NULL, map_size, prot, flags, fd, offset);
Py_END_ALLOW_THREADS
+ int saved_errno = errno;
if (devzero != -1) {
close(devzero);
}
if (m_obj->data == (char *)-1) {
m_obj->data = NULL;
Py_DECREF(m_obj);
+ errno = saved_errno;
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}
&NewWaitObject, Object, PostToQueueCallback, pdata, Milliseconds,
WT_EXECUTEINWAITTHREAD | WT_EXECUTEONLYONCE))
{
+ SetFromWindowsErr(0);
PyMem_RawFree(pdata);
- return SetFromWindowsErr(0);
+ return NULL;
}
return Py_BuildValue(F_HANDLE, NewWaitObject);
return NULL;
}
if (!len) {
+ PyErr_SetFromWindowsErr(0);
if (wbuf2 != wbuf)
PyMem_RawFree(wbuf2);
- return PyErr_SetFromWindowsErr(0);
+ return NULL;
}
PyObject *resobj = PyUnicode_FromWideChar(wbuf2, len);
return PyErr_NoMemory();
}
if (cwd == NULL) {
+ posix_error();
PyMem_RawFree(buf);
- return posix_error();
+ return NULL;
}
PyObject *obj;
int error = GetLastError();
if (error == ERROR_FILE_NOT_FOUND)
goto exit;
- Py_DECREF(list);
- list = path_error(path);
+ path_error(path);
+ Py_CLEAR(list);
goto exit;
}
do {
Py_SETREF(v, PyUnicode_EncodeFSDefault(v));
}
if (v == NULL) {
- Py_SETREF(list, NULL);
+ Py_CLEAR(list);
break;
}
if (PyList_Append(list, v) != 0) {
Py_DECREF(v);
- Py_SETREF(list, NULL);
+ Py_CLEAR(list);
break;
}
Py_DECREF(v);
/* FindNextFile sets error to ERROR_NO_MORE_FILES if
it got to the end of the directory. */
if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
- Py_DECREF(list);
- list = path_error(path);
+ path_error(path);
+ Py_CLEAR(list);
goto exit;
}
} while (result == TRUE);
if (hFindFile != INVALID_HANDLE_VALUE) {
if (FindClose(hFindFile) == FALSE) {
if (list != NULL) {
- Py_DECREF(list);
- list = path_error(path);
+ path_error(path);
+ Py_CLEAR(list);
}
}
}
}
if (dirp == NULL) {
- list = path_error(path);
+ path_error(path);
+ list = NULL;
#ifdef HAVE_FDOPENDIR
if (fd != -1) {
Py_BEGIN_ALLOW_THREADS
if (errno == 0) {
break;
} else {
- Py_DECREF(list);
- list = path_error(path);
+ path_error(path);
+ Py_CLEAR(list);
goto exit;
}
}
/* If we get here it's definitely an error */
+ posix_error();
free_string_array(argvlist, argc);
- return posix_error();
+ return NULL;
}
}
errno = posix_spawn_file_actions_addopen(file_actionsp,
fd, PyBytes_AS_STRING(path), oflag, (mode_t)mode);
- Py_DECREF(path);
if (errno) {
posix_error();
+ Py_DECREF(path);
goto fail;
}
+ Py_DECREF(path);
break;
}
case POSIX_SPAWN_CLOSE: {
_Py_END_SUPPRESS_IPH
Py_END_ALLOW_THREADS
+ int saved_errno = errno;
free_string_array(argvlist, argc);
- if (spawnval == -1)
- return posix_error();
- else
- return Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
+ if (spawnval == -1) {
+ errno = saved_errno;
+ posix_error();
+ return NULL;
+ }
+ return Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
}
/*[clinic input]
}
PyOS_BeforeFork();
pid = fork1();
+ int saved_errno = errno;
if (pid == 0) {
/* child: this clobbers and resets the import lock. */
PyOS_AfterFork_Child();
/* parent: release the import lock. */
PyOS_AfterFork_Parent();
}
- if (pid == -1)
+ if (pid == -1) {
+ errno = saved_errno;
return posix_error();
+ }
return PyLong_FromPid(pid);
}
#endif /* HAVE_FORK1 */
}
PyOS_BeforeFork();
pid = fork();
+ int saved_errno = errno;
if (pid == 0) {
/* child: this clobbers and resets the import lock. */
PyOS_AfterFork_Child();
/* parent: release the import lock. */
PyOS_AfterFork_Parent();
}
- if (pid == -1)
+ if (pid == -1) {
+ errno = saved_errno;
return posix_error();
+ }
return PyLong_FromPid(pid);
}
#endif /* HAVE_FORK */
/* change permission of slave */
if (grantpt(master_fd) < 0) {
+ int saved_errno = errno;
PyOS_setsig(SIGCHLD, sig_saved);
+ errno = saved_errno;
goto posix_error;
}
/* unlock slave */
if (unlockpt(master_fd) < 0) {
+ int saved_errno = errno;
PyOS_setsig(SIGCHLD, sig_saved);
+ errno = saved_errno;
goto posix_error;
}
n = getgroups(n, grouplist);
if (n == -1) {
+ posix_error();
PyMem_Free(grouplist);
- return posix_error();
+ return NULL;
}
PyObject *result = PyList_New(n);
}
if (setgroups(len, grouplist) < 0) {
+ posix_error();
PyMem_Free(grouplist);
- return posix_error();
+ return NULL;
}
PyMem_Free(grouplist);
Py_RETURN_NONE;
Py_END_ALLOW_THREADS
} while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
+ int saved_errno = errno;
iov_cleanup(iov, buf, cnt);
if (n < 0) {
- if (!async_err)
+ if (!async_err) {
+ errno = saved_errno;
posix_error();
+ }
return -1;
}
} while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
if (n < 0) {
+ if (!async_err) {
+ posix_error();
+ }
Py_DECREF(buffer);
- return (!async_err) ? posix_error() : NULL;
+ return NULL;
}
if (n != length)
_PyBytes_Resize(&buffer, n);
#endif
+ int saved_errno = errno;
iov_cleanup(iov, buf, cnt);
if (n < 0) {
if (!async_err) {
+ errno = saved_errno;
posix_error();
}
return -1;
} while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
_Py_END_SUPPRESS_IPH
+ int saved_errno = errno;
if (sf.headers != NULL)
iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
if (sf.trailers != NULL)
iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
if (ret < 0) {
- if ((errno == EAGAIN) || (errno == EBUSY)) {
+ if ((saved_errno == EAGAIN) || (saved_errno == EBUSY)) {
if (sbytes != 0) {
// some data has been sent
goto done;
}
- else {
- // no data has been sent; upper application is supposed
- // to retry on EAGAIN or EBUSY
- return posix_error();
- }
+ // no data has been sent; upper application is supposed
+ // to retry on EAGAIN or EBUSY
}
- return (!async_err) ? posix_error() : NULL;
+ if (!async_err) {
+ errno = saved_errno;
+ posix_error();
+ }
+ return NULL;
}
goto done;
Py_END_ALLOW_THREADS
} while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
- iov_cleanup(iov, buf, cnt);
if (result < 0 && !async_err)
posix_error();
+ iov_cleanup(iov, buf, cnt);
return result;
}
#endif /* HAVE_WRITEV */
#endif
- iov_cleanup(iov, buf, cnt);
if (result < 0) {
if (!async_err) {
posix_error();
}
- return -1;
+ result = -1;
}
+ iov_cleanup(iov, buf, cnt);
return result;
}
Prefer _wputenv() to be compatible with C libraries using CRT
variables and CRT functions using these variables (ex: getenv()). */
int err = _wputenv(env);
- PyMem_Free(env);
if (err) {
posix_error();
+ PyMem_Free(env);
return NULL;
}
+ PyMem_Free(env);
Py_RETURN_NONE;
}
Py_END_ALLOW_THREADS;
if (result < 0) {
- Py_DECREF(buffer);
- if (errno == ERANGE)
+ if (errno == ERANGE) {
+ Py_DECREF(buffer);
continue;
+ }
path_error(path);
+ Py_DECREF(buffer);
return NULL;
}
}
Py_END_ALLOW_THREADS
}
+
+ int saved_errno = errno;
#if defined(MS_WINDOWS)
PyMem_Free(path);
#else
Py_DECREF(ub);
#endif
- if (result != 0)
- return path_object_error(self->path);
+ if (result != 0) {
+ errno = saved_errno;
+ path_object_error(self->path);
+ return NULL;
+ }
return _pystat_fromstructstat(module, &st);
}
wchar_t *path = PyUnicode_AsWideCharString(unicode, NULL);
Py_DECREF(unicode);
result = LSTAT(path, &stat);
+
+ int saved_errno = errno;
PyMem_Free(path);
- if (result != 0)
+ if (result != 0) {
+ errno = saved_errno;
return path_object_error(self->path);
+ }
self->win32_file_index = stat.st_ino;
self->got_file_index = 1;
iterator->handle = FindFirstFileW(path_strW, &iterator->file_data);
Py_END_ALLOW_THREADS
- PyMem_Free(path_strW);
-
if (iterator->handle == INVALID_HANDLE_VALUE) {
path_error(&iterator->path);
+ PyMem_Free(path_strW);
goto error;
}
+ PyMem_Free(path_strW);
#else /* POSIX */
errno = 0;
#ifdef HAVE_FDOPENDIR
self->epfd = fd;
}
if (self->epfd < 0) {
- Py_DECREF(self);
PyErr_SetFromErrno(PyExc_OSError);
+ Py_DECREF(self);
return NULL;
}
self->kqfd = fd;
}
if (self->kqfd < 0) {
- Py_DECREF(self);
PyErr_SetFromErrno(PyExc_OSError);
+ Py_DECREF(self);
return NULL;
}
}
if (!SetHandleInformation((HANDLE)fd, HANDLE_FLAG_INHERIT, 0)) {
- closesocket(fd);
PyErr_SetFromWindowsErr(0);
+ closesocket(fd);
return -1;
}
name,
&size))
{
+ PyErr_SetFromWindowsErr(0);
PyMem_Free(name);
- return PyErr_SetFromWindowsErr(0);
+ return NULL;
}
result = PyUnicode_FromWideChar(name, size);
}
if (!SetHandleInformation((HANDLE)newfd, HANDLE_FLAG_INHERIT, 0)) {
- closesocket(newfd);
PyErr_SetFromWindowsErr(0);
+ closesocket(newfd);
return NULL;
}
#else
/* inet_ntop guarantee NUL-termination of resulting string. */
retval = inet_ntop(af, packed_ip.buf, ip, sizeof(ip));
- PyBuffer_Release(&packed_ip);
if (!retval) {
PyErr_SetFromErrno(PyExc_OSError);
+ PyBuffer_Release(&packed_ip);
return NULL;
} else {
+ PyBuffer_Release(&packed_ip);
return PyUnicode_FromString(retval);
}
}
ni = if_nameindex();
if (ni == NULL) {
- Py_DECREF(list);
PyErr_SetFromErrno(PyExc_OSError);
+ Py_DECREF(list);
return NULL;
}
if (err != ERROR_NO_UNICODE_TRANSLATION
&& err != ERROR_INSUFFICIENT_BUFFER)
{
- PyErr_SetFromWindowsErr(0);
+ PyErr_SetFromWindowsErr(err);
goto error;
}
insize++;
Py_END_ALLOW_THREADS
} while (f == NULL
&& errno == EINTR && !(async_err = PyErr_CheckSignals()));
+ int saved_errno = errno;
PyMem_Free(wpath);
#else
PyObject *bytes;
Py_END_ALLOW_THREADS
} while (f == NULL
&& errno == EINTR && !(async_err = PyErr_CheckSignals()));
-
+ int saved_errno = errno;
Py_DECREF(bytes);
#endif
if (async_err)
return NULL;
if (f == NULL) {
+ errno = saved_errno;
PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
return NULL;
}