From: Peter Xu <peterx@redhat.com>
To: qemu-devel@nongnu.org
Cc: Paolo Bonzini <pbonzini@redhat.com>,
Markus Armbruster <armbru@redhat.com>,
peterx@redhat.com, "Dr. David Alan Gilbert" <dgilbert@redhat.com>
Subject: [Qemu-devel] [PATCH v7 4/4] utils: remove assert in size_to_str()
Date: Thu, 11 May 2017 20:22:38 +0800 [thread overview]
Message-ID: <1494505358-15287-5-git-send-email-peterx@redhat.com> (raw)
In-Reply-To: <1494505358-15287-1-git-send-email-peterx@redhat.com>
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 <peterx@redhat.com>
---
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
next prev parent reply other threads:[~2017-05-11 12:23 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-05-11 12:22 [Qemu-devel] [PATCH v7 0/4] ramblock: add hmp command "info ramblock" Peter Xu
2017-05-11 12:22 ` [Qemu-devel] [PATCH v7 1/4] ramblock: add RAMBLOCK_FOREACH() Peter Xu
2017-05-11 13:01 ` Dr. David Alan Gilbert
2017-05-11 12:22 ` [Qemu-devel] [PATCH v7 2/4] utils: provide size_to_str() Peter Xu
2017-05-11 13:05 ` Dr. David Alan Gilbert
2017-05-12 4:12 ` Peter Xu
2017-05-11 12:22 ` [Qemu-devel] [PATCH v7 3/4] ramblock: add new hmp command "info ramblock" Peter Xu
2017-05-11 12:22 ` Peter Xu [this message]
2017-05-11 15:03 ` [Qemu-devel] [PATCH v7 4/4] utils: remove assert in size_to_str() Eric Blake
2017-05-11 18:22 ` Markus Armbruster
2017-05-11 18:26 ` Eric Blake
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1494505358-15287-5-git-send-email-peterx@redhat.com \
--to=peterx@redhat.com \
--cc=armbru@redhat.com \
--cc=dgilbert@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).