gspawn: Fix errno saving in Windows implementation

The error number was saved after some g_debug() and g_free() calls, in
various places, which meant it could have been overwritten since the
error we care about happened.

https://gitlab.gnome.org/GNOME/glib/issues/303

Signed-off-by: Philip Withnall <withnall@endlessm.com>
This commit is contained in:
Philip Withnall
2018-06-12 15:33:35 +01:00
parent fded2090e2
commit f4d2051fc1

View File

@@ -316,6 +316,7 @@ read_helper_report (int fd,
while (bytes < sizeof(gintptr)*2) while (bytes < sizeof(gintptr)*2)
{ {
gint chunk; gint chunk;
int errsv;
if (debug) if (debug)
g_print ("%s:read_helper_report: read %" G_GSIZE_FORMAT "...\n", g_print ("%s:read_helper_report: read %" G_GSIZE_FORMAT "...\n",
@@ -324,14 +325,13 @@ read_helper_report (int fd,
chunk = read (fd, ((gchar*)report) + bytes, chunk = read (fd, ((gchar*)report) + bytes,
sizeof(gintptr)*2 - bytes); sizeof(gintptr)*2 - bytes);
errsv = errno;
if (debug) if (debug)
g_print ("...got %d bytes\n", chunk); g_print ("...got %d bytes\n", chunk);
if (chunk < 0) if (chunk < 0)
{ {
int errsv = errno;
/* Some weird shit happened, bail out */ /* Some weird shit happened, bail out */
g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED, g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
_("Failed to read from child pipe (%s)"), _("Failed to read from child pipe (%s)"),
@@ -429,7 +429,7 @@ do_spawn_directly (gint *exit_status,
const int mode = (exit_status == NULL) ? P_NOWAIT : P_WAIT; const int mode = (exit_status == NULL) ? P_NOWAIT : P_WAIT;
char **new_argv; char **new_argv;
gintptr rc = -1; gintptr rc = -1;
int saved_errno; int errsv;
GError *conv_error = NULL; GError *conv_error = NULL;
gint conv_error_index; gint conv_error_index;
wchar_t *wargv0, **wargv, **wenvp; wchar_t *wargv0, **wargv, **wenvp;
@@ -481,17 +481,17 @@ do_spawn_directly (gint *exit_status,
else else
rc = _wspawnv (mode, wargv0, (const wchar_t **) wargv); rc = _wspawnv (mode, wargv0, (const wchar_t **) wargv);
errsv = errno;
g_free (wargv0); g_free (wargv0);
g_strfreev ((gchar **) wargv); g_strfreev ((gchar **) wargv);
g_strfreev ((gchar **) wenvp); g_strfreev ((gchar **) wenvp);
saved_errno = errno; if (rc == -1 && errsv != 0)
if (rc == -1 && saved_errno != 0)
{ {
g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED, g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
_("Failed to execute child process (%s)"), _("Failed to execute child process (%s)"),
g_strerror (saved_errno)); g_strerror (errsv));
return FALSE; return FALSE;
} }
@@ -532,7 +532,7 @@ do_spawn_with_pipes (gint *exit_status,
char **new_argv; char **new_argv;
int i; int i;
gintptr rc = -1; gintptr rc = -1;
int saved_errno; int errsv;
int argc; int argc;
int stdin_pipe[2] = { -1, -1 }; int stdin_pipe[2] = { -1, -1 };
int stdout_pipe[2] = { -1, -1 }; int stdout_pipe[2] = { -1, -1 };
@@ -752,7 +752,7 @@ do_spawn_with_pipes (gint *exit_status,
else else
rc = _wspawnvp (P_NOWAIT, whelper, (const wchar_t **) wargv); rc = _wspawnvp (P_NOWAIT, whelper, (const wchar_t **) wargv);
saved_errno = errno; errsv = errno;
g_free (whelper); g_free (whelper);
g_strfreev ((gchar **) wargv); g_strfreev ((gchar **) wargv);
@@ -774,11 +774,11 @@ do_spawn_with_pipes (gint *exit_status,
g_free (new_argv); g_free (new_argv);
/* Check if gspawn-win32-helper couldn't be run */ /* Check if gspawn-win32-helper couldn't be run */
if (rc == -1 && saved_errno != 0) if (rc == -1 && errsv != 0)
{ {
g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED, g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
_("Failed to execute helper program (%s)"), _("Failed to execute helper program (%s)"),
g_strerror (saved_errno)); g_strerror (errsv));
goto cleanup_and_fail; goto cleanup_and_fail;
} }