mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-08-02 07:23:41 +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:
@@ -748,6 +748,7 @@ handle_overwrite_open (const char *filename,
|
||||
int open_flags;
|
||||
int res;
|
||||
int mode;
|
||||
int errsv;
|
||||
|
||||
mode = mode_from_flags_or_info (flags, reference_info);
|
||||
|
||||
@@ -763,12 +764,13 @@ handle_overwrite_open (const char *filename,
|
||||
#ifdef O_NOFOLLOW
|
||||
is_symlink = FALSE;
|
||||
fd = g_open (filename, open_flags | O_NOFOLLOW, mode);
|
||||
errsv = errno;
|
||||
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
|
||||
if (fd == -1 && errno == EMLINK)
|
||||
if (fd == -1 && errsv == EMLINK)
|
||||
#elif defined(__NetBSD__)
|
||||
if (fd == -1 && errno == EFTYPE)
|
||||
if (fd == -1 && errsv == EFTYPE)
|
||||
#else
|
||||
if (fd == -1 && errno == ELOOP)
|
||||
if (fd == -1 && errsv == ELOOP)
|
||||
#endif
|
||||
{
|
||||
/* Could be a symlink, or it could be a regular ELOOP error,
|
||||
@@ -778,13 +780,13 @@ handle_overwrite_open (const char *filename,
|
||||
}
|
||||
#else
|
||||
fd = g_open (filename, open_flags, mode);
|
||||
errsv = errno;
|
||||
/* This is racy, but we do it as soon as possible to minimize the race */
|
||||
is_symlink = g_file_test (filename, G_FILE_TEST_IS_SYMLINK);
|
||||
#endif
|
||||
|
||||
|
||||
if (fd == -1)
|
||||
{
|
||||
int errsv = errno;
|
||||
char *display_name = g_filename_display_name (filename);
|
||||
g_set_error (error, G_IO_ERROR,
|
||||
g_io_error_from_errno (errsv),
|
||||
@@ -799,10 +801,10 @@ handle_overwrite_open (const char *filename,
|
||||
#else
|
||||
res = fstat (fd, &original_stat);
|
||||
#endif
|
||||
errsv = errno;
|
||||
|
||||
if (res != 0)
|
||||
if (res != 0)
|
||||
{
|
||||
int errsv = errno;
|
||||
char *display_name = g_filename_display_name (filename);
|
||||
g_set_error (error, G_IO_ERROR,
|
||||
g_io_error_from_errno (errsv),
|
||||
|
Reference in New Issue
Block a user