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

@@ -25,7 +25,8 @@ my_pipe (int *fds)
{
if (pipe(fds) < 0)
{
fprintf (stderr, "Cannot create pipe %s\n", strerror (errno));
int errsv = errno;
fprintf (stderr, "Cannot create pipe %s\n", strerror (errsv));
exit (1);
}
}
@@ -121,7 +122,7 @@ input_callback (int source, int dest)
void
create_child (int pos)
{
int pid;
int pid, errsv;
int in_fds[2];
int out_fds[2];
@@ -129,6 +130,7 @@ create_child (int pos)
my_pipe (out_fds);
pid = fork ();
errsv = errno;
if (pid > 0) /* Parent */
{
@@ -150,7 +152,7 @@ create_child (int pos)
}
else /* Error */
{
fprintf (stderr,"Cannot fork: %s\n", strerror (errno));
fprintf (stderr,"Cannot fork: %s\n", strerror (errsv));
exit (1);
}
}