Consistently save errno immediately after the operation setting it

Prevent the situation where errno is set by function A, then function B
is called (which is typically _(), but could be anything else) and it
overwrites errno, then errno is checked by the caller.

errno is a horrific API, and we need to be careful to save its value as
soon as a function call (which might set it) returns. i.e. Follow the
pattern:
  int errsv, ret;
  ret = some_call_which_might_set_errno ();
  errsv = errno;

  if (ret < 0)
    puts (strerror (errsv));

This patch implements that pattern throughout GLib. There might be a few
places in the test code which still use errno directly. They should be
ported as necessary. It doesn’t modify all the call sites like this:
  if (some_call_which_might_set_errno () && errno == ESOMETHING)
since the refactoring involved is probably more harmful than beneficial
there. It does, however, refactor other call sites regardless of whether
they were originally buggy.

https://bugzilla.gnome.org/show_bug.cgi?id=785577
This commit is contained in:
Philip Withnall
2017-07-31 11:30:55 +01:00
parent 41a4a70b43
commit 5cddde1fb2
44 changed files with 337 additions and 166 deletions

View File

@@ -2694,9 +2694,12 @@ child_read (GIOChannel *io, GIOCondition cond, gpointer user_data)
{
for (total = 0; total < nread; total += nwrote)
{
int errsv;
nwrote = fwrite (buf + total, 1, nread - total, echo_file);
errsv = errno;
if (nwrote == 0)
g_error ("write failed: %s", g_strerror (errno));
g_error ("write failed: %s", g_strerror (errsv));
}
}
@@ -2817,13 +2820,18 @@ g_test_trap_fork (guint64 usec_timeout,
#ifdef G_OS_UNIX
int stdout_pipe[2] = { -1, -1 };
int stderr_pipe[2] = { -1, -1 };
int errsv;
test_trap_clear();
if (pipe (stdout_pipe) < 0 || pipe (stderr_pipe) < 0)
g_error ("failed to create pipes to fork test program: %s", g_strerror (errno));
{
errsv = errno;
g_error ("failed to create pipes to fork test program: %s", g_strerror (errsv));
}
test_trap_last_pid = fork ();
errsv = errno;
if (test_trap_last_pid < 0)
g_error ("failed to fork test program: %s", g_strerror (errno));
g_error ("failed to fork test program: %s", g_strerror (errsv));
if (test_trap_last_pid == 0) /* child */
{
int fd0 = -1;
@@ -2836,7 +2844,10 @@ g_test_trap_fork (guint64 usec_timeout,
g_error ("failed to open /dev/null for stdin redirection");
}
if (sane_dup2 (stdout_pipe[1], 1) < 0 || sane_dup2 (stderr_pipe[1], 2) < 0 || (fd0 >= 0 && sane_dup2 (fd0, 0) < 0))
g_error ("failed to dup2() in forked test program: %s", g_strerror (errno));
{
errsv = errno;
g_error ("failed to dup2() in forked test program: %s", g_strerror (errsv));
}
if (fd0 >= 3)
close (fd0);
if (stdout_pipe[1] >= 3)