summaryrefslogtreecommitdiff
path: root/shared
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2019-01-31 13:29:21 +0100
committerThomas Haller <thaller@redhat.com>2019-02-12 08:50:28 +0100
commit047998f80a8686f5200cd48d31349c977f9a50b7 (patch)
tree01e75fa5f724f0940efba561ef5420c9ff6078af /shared
parentb7bb7449738b8fd3eee545af7fa5efd004718431 (diff)
downloadNetworkManager-047998f80a8686f5200cd48d31349c977f9a50b7.tar.gz
all: cache errno in local variable before using it
Diffstat (limited to 'shared')
-rw-r--r--shared/nm-utils/nm-io-utils.c51
-rw-r--r--shared/nm-utils/nm-shared-utils.c8
2 files changed, 35 insertions, 24 deletions
diff --git a/shared/nm-utils/nm-io-utils.c b/shared/nm-utils/nm-io-utils.c
index 06f756c412..ac16ff97f2 100644
--- a/shared/nm-utils/nm-io-utils.c
+++ b/shared/nm-utils/nm-io-utils.c
@@ -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);
diff --git a/shared/nm-utils/nm-shared-utils.c b/shared/nm-utils/nm-shared-utils.c
index db97111193..860d4c18a6 100644
--- a/shared/nm-utils/nm-shared-utils.c
+++ b/shared/nm-utils/nm-shared-utils.c
@@ -1769,10 +1769,12 @@ nm_utils_fd_read_loop (int fd, void *buf, size_t nbytes, bool do_poll)
k = read (fd, p, nbytes);
if (k < 0) {
- if (errno == EINTR)
+ int errsv = errno;
+
+ if (errsv == EINTR)
continue;
- if (errno == EAGAIN && do_poll) {
+ if (errsv == EAGAIN && do_poll) {
/* We knowingly ignore any return value here,
* and expect that any error/EOF is reported
@@ -1782,7 +1784,7 @@ nm_utils_fd_read_loop (int fd, void *buf, size_t nbytes, bool do_poll)
continue;
}
- return n > 0 ? n : -errno;
+ return n > 0 ? n : -errsv;
}
if (k == 0)