From f4e9195149547b97c0064a50a0587e13d5e5fc55 Mon Sep 17 00:00:00 2001 From: Jim Warner Date: Mon, 22 Feb 2021 00:00:00 -0600 Subject: [PATCH] library: normalized the 'read_failed' guys across APIs This patch will condense some logic in those functions associated with the file input operations. The changes will not, for the most part, alter any generated code. More significantly (though not very) was the change to two 'strtoul' calls. Since the returned 'endptr' value isn't exploited, when that parm is set to NULL, we can save one instruction on each side of such calls (wow). Signed-off-by: Jim Warner --- proc/meminfo.c | 13 +++++-------- proc/vmstat.c | 11 ++++------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/proc/meminfo.c b/proc/meminfo.c index c1d9f0ee..e6f13640 100644 --- a/proc/meminfo.c +++ b/proc/meminfo.c @@ -646,7 +646,7 @@ static int meminfo_read_failed ( memset(&info->hist.new, 0, sizeof(struct meminfo_data)); if (-1 == info->meminfo_fd - && (info->meminfo_fd = open(MEMINFO_FILE, O_RDONLY)) == -1) + && (-1 == (info->meminfo_fd = open(MEMINFO_FILE, O_RDONLY)))) return 1; if (lseek(info->meminfo_fd, 0L, SEEK_SET) == -1) @@ -672,8 +672,7 @@ static int meminfo_read_failed ( static ENTRY e; // just to keep coverity off our backs (e.data) ENTRY *ep; - tail = strchr(head, ' '); - if (!tail) + if (!(tail = strchr(head, ' '))) break; *tail = '\0'; valptr = NULL; @@ -681,13 +680,11 @@ static int meminfo_read_failed ( e.key = head; if (hsearch_r(e, FIND, &ep, &info->hashtab)) valptr = ep->data; - - head = tail+1; + head = tail + 1; if (valptr) - *valptr = strtoul(head, &tail, 10); + *valptr = strtoul(head, NULL, 10); - tail = strchr(head, '\n'); - if (!tail) + if (!(tail = strchr(head, '\n'))) break; head = tail + 1; } diff --git a/proc/vmstat.c b/proc/vmstat.c index 7e47e657..f9334175 100644 --- a/proc/vmstat.c +++ b/proc/vmstat.c @@ -1170,7 +1170,7 @@ static int vmstat_read_failed ( memset(&info->hist.new, 0, sizeof(struct vmstat_data)); if (-1 == info->vmstat_fd - && (info->vmstat_fd = open(VMSTAT_FILE, O_RDONLY)) == -1) + && (-1 == (info->vmstat_fd = open(VMSTAT_FILE, O_RDONLY)))) return 1; if (lseek(info->vmstat_fd, 0L, SEEK_SET) == -1) @@ -1196,8 +1196,7 @@ static int vmstat_read_failed ( static ENTRY e; // just to keep coverity off our backs (e.data) ENTRY *ep; - tail = strchr(head, ' '); - if (!tail) + if (!(tail = strchr(head, ' '))) break; *tail = '\0'; valptr = NULL; @@ -1205,13 +1204,11 @@ static int vmstat_read_failed ( e.key = head; if (hsearch_r(e, FIND, &ep, &info->hashtab)) valptr = ep->data; - head = tail + 1; if (valptr) - *valptr = strtoul(head, &tail, 10); + *valptr = strtoul(head, NULL, 10); - tail = strchr(head, '\n'); - if (!tail) + if (!(tail = strchr(head, '\n'))) break; head = tail + 1; }