From 25f5947512bcc929c0f0acba899c288ba7547dbf Mon Sep 17 00:00:00 2001 From: "Bruce A. Mah" Date: Wed, 4 Oct 2017 10:15:19 -0700 Subject: [PATCH] Fix a problem formatting very large numbers. (#642) Avoid walking off the end of an array when trying to format a number larger than 1000T. Motivated by #641, as reported by @shingchuang, but slightly reimplemented. --- src/t_units.c | 7 +++++++ src/units.c | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/t_units.c b/src/t_units.c index b4a87c3..8fd8bd9 100644 --- a/src/t_units.c +++ b/src/t_units.c @@ -99,5 +99,12 @@ main(int argc, char **argv) unit_snprintf(s, 11, d, 'a'); assert(strncmp(s, "35.2 Tbit", 11) == 0); + d = 4.0 * 1024 * 1024 * 1024 * 1024 * 1024; + unit_snprintf(s, 11, d, 'A'); + assert(strncmp(s, "4096 TByte", 11) == 0); + + unit_snprintf(s, 11, d, 'a'); + assert(strncmp(s, "36029 Tbit", 11) == 0); + return 0; } diff --git a/src/units.c b/src/units.c index f9533f0..7376a0b 100644 --- a/src/units.c +++ b/src/units.c @@ -303,14 +303,14 @@ extern "C" if (isupper((int) inFormat)) { - while (tmpNum >= 1024.0 && conv <= TERA_CONV) + while (tmpNum >= 1024.0 && conv < TERA_CONV) { tmpNum /= 1024.0; conv++; } } else { - while (tmpNum >= 1000.0 && conv <= TERA_CONV) + while (tmpNum >= 1000.0 && conv < TERA_CONV) { tmpNum /= 1000.0; conv++;