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.
This commit is contained in:
Bruce A. Mah 2017-10-04 10:15:19 -07:00 committed by GitHub
parent 2bc3d2ee5d
commit 25f5947512
2 changed files with 9 additions and 2 deletions

View File

@ -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;
}

View File

@ -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++;