mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-08-04 08:23:38 +02:00
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:
@@ -23,7 +23,8 @@ io_pipe (GIOChannel **channels)
|
||||
|
||||
if (pipe(fds) < 0)
|
||||
{
|
||||
fprintf (stderr, "Cannot create pipe %s\n", g_strerror (errno));
|
||||
int errsv = errno;
|
||||
fprintf (stderr, "Cannot create pipe %s\n", g_strerror (errsv));
|
||||
exit (1);
|
||||
}
|
||||
|
||||
@@ -134,7 +135,7 @@ input_callback (GIOChannel *source,
|
||||
static void
|
||||
create_child (void)
|
||||
{
|
||||
int pid;
|
||||
int pid, errsv;
|
||||
GIOChannel *in_channels[2];
|
||||
GIOChannel *out_channels[2];
|
||||
GSource *source;
|
||||
@@ -143,6 +144,7 @@ create_child (void)
|
||||
io_pipe (out_channels);
|
||||
|
||||
pid = fork ();
|
||||
errsv = errno;
|
||||
|
||||
if (pid > 0) /* Parent */
|
||||
{
|
||||
@@ -172,7 +174,7 @@ create_child (void)
|
||||
}
|
||||
else /* Error */
|
||||
{
|
||||
fprintf (stderr, "Cannot fork: %s\n", g_strerror (errno));
|
||||
fprintf (stderr, "Cannot fork: %s\n", g_strerror (errsv));
|
||||
exit (1);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user