From 03a7850de4c6677bafd645645584f0e01b11e4cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Wed, 19 Jun 2019 16:44:37 +0300 Subject: [PATCH] Fix mxb::to_binary_size The function now handles negative and zero values. --- maxutils/maxbase/include/maxbase/format.hh | 2 +- maxutils/maxbase/src/format.cc | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/maxutils/maxbase/include/maxbase/format.hh b/maxutils/maxbase/include/maxbase/format.hh index f64443cd7..27b0aa6f3 100644 --- a/maxutils/maxbase/include/maxbase/format.hh +++ b/maxutils/maxbase/include/maxbase/format.hh @@ -28,7 +28,7 @@ namespace maxbase * * @return Value as a human readable size e.g. 5.01MiB */ -std::string to_binary_size(int64_t size); +std::string to_binary_size(size_t size); /** * Format parameters to a string. Uses printf-formatting. diff --git a/maxutils/maxbase/src/format.cc b/maxutils/maxbase/src/format.cc index 54be0ee8c..2d3f700a1 100644 --- a/maxutils/maxbase/src/format.cc +++ b/maxutils/maxbase/src/format.cc @@ -58,10 +58,10 @@ const char* get_binary_size_suffix(int i) namespace maxbase { -std::string to_binary_size(int64_t size) +std::string to_binary_size(size_t size) { // Calculate log1024(size) and round it up - int idx = floor(log(size) / log(1024)); + int idx = floor(log(size ? size : 1) / log(1024)); double num = size / pow(1024, idx); char buf[200]; // Enough for all possible values snprintf(buf, sizeof(buf), "%.2lf%s", num, get_binary_size_suffix(idx));