all repos — slstatus @ a36cb96f274c6dd829b140ffa604074477c7557a

my build of slstatus (tools.suckless.org/slstatus/)

swap_*(): add more error tests
raiz raiz@firemail.cc
Tue, 27 Dec 2016 19:26:04 +0300
commit

a36cb96f274c6dd829b140ffa604074477c7557a

parent

862d158b7c3f1bd138c00e1bd29de87d3fc89f15

1 files changed, 16 insertions(+), 3 deletions(-)

jump to
M slstatus.cslstatus.c

@@ -439,9 +439,15 @@ if (fp == NULL) {

warn("Failed to open file /proc/meminfo"); return smprintf(UNKNOWN_STR); } - bytes_read = fread(buf, sizeof(char), sizeof(buf), fp); + + if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) { + warn("swap_free: read error"); + fclose(fp); + } + buf[bytes_read] = '\0'; fclose(fp); + if (bytes_read == 0 || bytes_read == sizeof(buf)) { warn("Failed to read from /proc/meminfo"); return smprintf(UNKNOWN_STR);

@@ -473,9 +479,11 @@ if (fp == NULL) {

warn("Failed to open file /proc/meminfo"); return smprintf(UNKNOWN_STR); } + bytes_read = fread(buf, sizeof(char), sizeof(buf), fp); buf[bytes_read] = '\0'; fclose(fp); + if (bytes_read == 0 || bytes_read == sizeof(buf)) { warn("Failed to read from /proc/meminfo"); return smprintf(UNKNOWN_STR);

@@ -487,11 +495,16 @@ if (total == 0) {

return smprintf(UNKNOWN_STR); } - match = strstr(buf, "SwapCached"); + if ((match = strstr(buf, "SwapCached")) == NULL) { + return smprintf("%s", UNKNOWN_STR); + } sscanf(match, "SwapCached: %ld kB\n", &cached); - match = strstr(buf, "SwapFree"); + if ((match = strstr(buf, "SwapFree")) == NULL) { + return smprintf("%s", UNKNOWN_STR); + } sscanf(match, "SwapFree: %ld kB\n", &free); + return smprintf("%d%%", 100 * (total - free - cached) / total); }