[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH v7 4/4] utils: remove assert in size_to_str()
From: |
Peter Xu |
Subject: |
[Qemu-devel] [PATCH v7 4/4] utils: remove assert in size_to_str() |
Date: |
Thu, 11 May 2017 20:22:38 +0800 |
It's not very safe to assert in size_to_str(). Let's be inclusive.
It naturally allows negative values. Now it won't even limit on the
size, as long as double would allow.
Signed-off-by: Peter Xu <address@hidden>
---
util/cutils.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/util/cutils.c b/util/cutils.c
index fa5ddec..8df0963 100644
--- a/util/cutils.c
+++ b/util/cutils.c
@@ -622,7 +622,7 @@ const char *qemu_ether_ntoa(const MACAddr *mac)
/*
* Return human readable string for size @val.
- * @val must be between (-1000Eib, 1000EiB), exclusively.
+ * @val can be any value.
* Use IEC binary units like KiB, MiB, and so forth.
* Caller is responsible for passing it to g_free().
*/
@@ -640,7 +640,12 @@ char *size_to_str(double val)
*/
frexp(val / (1000.0 / 1024.0), &i);
i = (i - 1) / 10;
- assert(i < ARRAY_SIZE(suffixes));
+
+ /* Use the biggest possible suffix */
+ if (i > ARRAY_SIZE(suffixes) - 1) {
+ i = ARRAY_SIZE(suffixes) - 1;
+ }
+
div = 1ULL << (i * 10);
return g_strdup_printf("%0.3g %sB", val / div, suffixes[i]);
--
2.7.4