all: cache errno in local variable before using it

This commit is contained in:
Thomas Haller
2019-01-31 13:29:21 +01:00
parent b7bb744973
commit 047998f80a
34 changed files with 217 additions and 147 deletions

View File

@@ -36,10 +36,11 @@ _nm_printf (3, 4)
static int
_get_contents_error (GError **error, int errsv, const char *format, ...)
{
if (errsv < 0)
errsv = -errsv;
else if (!errsv)
errsv = errno;
if (errsv < 0) {
errsv = errsv == G_MININT
? G_MAXINT
: -errsv;
}
if (error) {
char *msg;
@@ -57,6 +58,12 @@ _get_contents_error (GError **error, int errsv, const char *format, ...)
}
return -errsv;
}
#define _get_contents_error_errno(error, ...) \
({ \
int _errsv = (errno); \
\
_get_contents_error (error, _errsv, __VA_ARGS__); \
})
static char *
_mem_realloc (char *old, gboolean do_bzero_mem, gsize cur_len, gsize new_len)
@@ -127,13 +134,14 @@ nm_utils_fd_get_contents (int fd,
struct stat stat_buf;
gs_free char *str = NULL;
const bool do_bzero_mem = NM_FLAGS_HAS (flags, NM_UTILS_FILE_GET_CONTENTS_FLAG_SECRET);
int errsv;
g_return_val_if_fail (fd >= 0, -EINVAL);
g_return_val_if_fail (contents, -EINVAL);
g_return_val_if_fail (!error || !*error, -EINVAL);
if (fstat (fd, &stat_buf) < 0)
return _get_contents_error (error, 0, "failure during fstat");
return _get_contents_error_errno (error, "failure during fstat");
if (!max_length) {
/* default to a very large size, but not extreme */
@@ -156,7 +164,7 @@ nm_utils_fd_get_contents (int fd,
if (n_read < 0) {
if (do_bzero_mem)
nm_explicit_bzero (str, n_stat);
return _get_contents_error (error, n_read, "error reading %zu bytes from file descriptor", n_stat);
return _get_contents_error (error, -n_read, "error reading %zu bytes from file descriptor", n_stat);
}
str[n_read] = '\0';
@@ -176,19 +184,19 @@ nm_utils_fd_get_contents (int fd,
else {
fd2 = fcntl (fd, F_DUPFD_CLOEXEC, 0);
if (fd2 < 0)
return _get_contents_error (error, 0, "error during dup");
return _get_contents_error_errno (error, "error during dup");
}
if (!(f = fdopen (fd2, "r"))) {
errsv = errno;
nm_close (fd2);
return _get_contents_error (error, 0, "failure during fdopen");
return _get_contents_error (error, errsv, "failure during fdopen");
}
n_have = 0;
n_alloc = 0;
while (!feof (f)) {
int errsv;
gsize n_read;
n_read = fread (buf, 1, sizeof (buf), f);
@@ -395,20 +403,21 @@ nm_utils_file_set_contents (const char *filename,
* guarantee the data is written to the disk before the metadata.)
*/
if ( lstat (filename, &statbuf) == 0
&& statbuf.st_size > 0
&& fsync (fd) != 0) {
errsv = errno;
&& statbuf.st_size > 0) {
if (fsync (fd) != 0) {
errsv = errno;
nm_close (fd);
unlink (tmp_name);
nm_close (fd);
unlink (tmp_name);
g_set_error (error,
G_FILE_ERROR,
g_file_error_from_errno (errsv),
"failed to fsync %s: %s",
tmp_name,
g_strerror (errsv));
return FALSE;
g_set_error (error,
G_FILE_ERROR,
g_file_error_from_errno (errsv),
"failed to fsync %s: %s",
tmp_name,
g_strerror (errsv));
return FALSE;
}
}
nm_close (fd);