From 8bcf5491d805ba0e97d0904040218f15b5c478a6 Mon Sep 17 00:00:00 2001 From: Jordan Galby Date: Thu, 8 Oct 2020 09:15:43 +0000 Subject: 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 . --- src/print_mem.c | 6 ++++-- 1 file 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 #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) { -- cgit v1.2.3