summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJordan Galby <gravemind2a@gmail.com>2020-10-08 09:15:43 +0000
committerGitHub <noreply@github.com>2020-10-08 11:15:43 +0200
commit8bcf5491d805ba0e97d0904040218f15b5c478a6 (patch)
treefebf6193e0ce9ae18633d7db089f3ab20871b0af
parent3451a0d9fc81c69d4dcaf93b511459b164df05e8 (diff)
Fix crash in print_memory (#427)
Fixes a stack-overflow when memory is, for example, "1020.17 TiB". This fix limits the max number of memory decimals to 4. The crash was probably introduced in 066e813331 .
-rw-r--r--src/print_mem.c6
1 files changed, 4 insertions, 2 deletions
diff --git a/src/print_mem.c b/src/print_mem.c
index 4a521d7..b2cd617 100644
--- a/src/print_mem.c
+++ b/src/print_mem.c
@@ -7,7 +7,8 @@
#include <yajl/yajl_version.h>
#include "i3status.h"
-#define STRING_SIZE 10
+#define MAX_DECIMALS 4
+#define STRING_SIZE ((sizeof "1023. TiB") + MAX_DECIMALS)
#define BINARY_BASE 1024UL
@@ -32,7 +33,8 @@ static int print_bytes_human(char *outwalk, unsigned long bytes, const char *uni
base /= BINARY_BASE;
exponent += 1;
}
- return sprintf(outwalk, "%.*f %s", decimals, base, iec_symbols[exponent]);
+ const int prec = decimals > MAX_DECIMALS ? MAX_DECIMALS : decimals;
+ return sprintf(outwalk, "%.*f %s", prec, base, iec_symbols[exponent]);
}
static int print_percentage(char *outwalk, float percent) {