From a6115bfff4e91766d7295658d864d391cb9d73da Mon Sep 17 00:00:00 2001 From: Jim Warner Date: Sun, 3 Jul 2016 00:00:00 -0500 Subject: [PATCH] library: some tweaks to 2 file read functions, 3rd gen Ever since their introduction, plus continuing through several evolutions, both the meminfo and vmstat 'read' functions employed a 'do while' loop for /proc access. However, that loop construct was wrong since identical tests were already done (twice!) within each loop body itself, then accompanied by its own 'break' statement. So, we will now transform them both into forever loops which will help us to emphasize such break statements. [ plus, let's return an error should nothing be read ] [ lastly, eliminate 1 erroneous PROCPS_EXPORT prefix ] Reference(s): . original meminfo introduction commit a20e88e4e72067c1138721c6e15e2d1130bc9595 . original vmstat introduction commit a410e236abb47c7c43194e61d0566686f81513af Signed-off-by: Jim Warner --- proc/meminfo.c | 11 +++++------ proc/vmstat.c | 6 +++--- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/proc/meminfo.c b/proc/meminfo.c index 9be12d4c..302c2c68 100644 --- a/proc/meminfo.c +++ b/proc/meminfo.c @@ -534,7 +534,7 @@ static int make_hash_failed ( * Read the data out of /proc/meminfo putting the information * into the supplied info structure */ -PROCPS_EXPORT int read_meminfo_failed ( +static int read_meminfo_failed ( struct procps_meminfo *info) { /* a 'memory history reference' macro for readability, @@ -570,11 +570,11 @@ PROCPS_EXPORT int read_meminfo_failed ( break; } if (size == 0) - return 0; + return -1; buf[size] = '\0'; head = buf; - do { + for (;;) { static ENTRY e; // just to keep coverity off our backs (e.data) ENTRY *ep; @@ -589,16 +589,15 @@ PROCPS_EXPORT int read_meminfo_failed ( valptr = ep->data; head = tail+1; - if (valptr) { + if (valptr) *valptr = strtoul(head, &tail, 10); - } tail = strchr(head, '\n'); if (!tail) break; head = tail + 1; - } while(tail); + } if (0 == mHr(MemAvailable)) mHr(MemAvailable) = mHr(MemFree); diff --git a/proc/vmstat.c b/proc/vmstat.c index 6de294a8..e9b4506e 100644 --- a/proc/vmstat.c +++ b/proc/vmstat.c @@ -995,11 +995,11 @@ static int read_vmstat_failed ( break; } if (size == 0) - return 0; + return -1; buf[size] = '\0'; head = buf; - do { + for (;;) { static ENTRY e; // just to keep coverity off our backs (e.data) ENTRY *ep; @@ -1022,7 +1022,7 @@ static int read_vmstat_failed ( break; head = tail + 1; - } while(tail); + } // let's not distort the deltas the first time thru ... if (!info->vmstat_was_read)