bpo-32890, os: Use errno instead of GetLastError() in execve() and truncate() (GH-5784)
path_error() uses GetLastError() on Windows, but some os functions
are implemented via CRT APIs which report errors via errno.
This may result in raising OSError with invalid error code (such
as zero).
Introduce posix_path_error() function and use it where appropriate.
(cherry picked from commit 834603112e6ca35944dd21105b01fca562dc3241)
Co-authored-by: Alexey Izbyshev <izbyshev@ispras.ru>
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index bbbff1e..0c48a66 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -1422,13 +1422,19 @@
#endif /* MS_WINDOWS */
static PyObject *
+posix_path_object_error(PyObject *path)
+{
+ return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
+}
+
+static PyObject *
path_object_error(PyObject *path)
{
#ifdef MS_WINDOWS
return PyErr_SetExcFromWindowsErrWithFilenameObject(
PyExc_OSError, 0, path);
#else
- return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
+ return posix_path_object_error(path);
#endif
}
@@ -1450,6 +1456,12 @@
}
static PyObject *
+posix_path_error(path_t *path)
+{
+ return posix_path_object_error(path->object);
+}
+
+static PyObject *
path_error2(path_t *path, path_t *path2)
{
return path_object_error2(path->object, path2->object);
@@ -5097,7 +5109,7 @@
/* If we get here it's definitely an error */
- path_error(path);
+ posix_path_error(path);
free_string_array(envlist, envc);
fail:
@@ -9060,7 +9072,7 @@
_Py_END_SUPPRESS_IPH
Py_END_ALLOW_THREADS
if (result < 0)
- return path_error(path);
+ return posix_path_error(path);
Py_RETURN_NONE;
}