From: Markus Armbruster <armbru@redhat.com>
To: qemu-devel@nongnu.org
Cc: Fangrui Song <i@maskray.me>,
richard.henderson@linaro.org, dgilbert@redhat.com,
quintela@redhat.com
Subject: [PATCH v3 2/2] util/cutils: Fix incorrect integer->float conversion caught by clang
Date: Fri, 22 Nov 2019 09:00:39 +0100 [thread overview]
Message-ID: <20191122080039.12771-3-armbru@redhat.com> (raw)
In-Reply-To: <20191122080039.12771-1-armbru@redhat.com>
From: Fangrui Song <i@maskray.me>
Clang does not like do_strtosz()'s code to guard against overflow:
qemu/util/cutils.c:245:23: error: implicit conversion from 'unsigned long' to 'double' changes value from 18446744073709550592 to 18446744073709551616 [-Werror,-Wimplicit-int-float-conversion]
The warning will be enabled by default in clang 10. It is not
available for clang <= 9.
val * mul >= 0xfffffffffffffc00 is indeed wrong. 0xfffffffffffffc00
is not representable exactly as double. It's half-way between the
representable values 0xfffffffffffff800 and 0x10000000000000000.
Which one we get is implementation-defined. Bad.
We want val * mul > (the largest uint64_t exactly representable as
double). That's 0xfffffffffffff800. Write it as nextafter(0x1p64, 0)
with a suitable comment.
Signed-off-by: Fangrui Song <i@maskray.me>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
[Patch split, commit message improved]
Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
util/cutils.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/util/cutils.c b/util/cutils.c
index fd591cadf0..77acadc70a 100644
--- a/util/cutils.c
+++ b/util/cutils.c
@@ -239,10 +239,12 @@ static int do_strtosz(const char *nptr, const char **end,
goto out;
}
/*
- * Values >= 0xfffffffffffffc00 overflow uint64_t after their trip
- * through double (53 bits of precision).
+ * Values near UINT64_MAX overflow to 2**64 when converting to double
+ * precision. Compare against the maximum representable double precision
+ * value below 2**64, computed as "the next value after 2**64 (0x1p64) in
+ * the direction of 0".
*/
- if ((val * mul >= 0xfffffffffffffc00) || val < 0) {
+ if ((val * mul > nextafter(0x1p64, 0)) || val < 0) {
retval = -ERANGE;
goto out;
}
--
2.21.0
next prev parent reply other threads:[~2019-11-22 8:06 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-11-22 8:00 [PATCH v3 0/2] Fix incorrect integer->float conversion caught by clang Markus Armbruster
2019-11-22 8:00 ` [PATCH v3 1/2] migration: " Markus Armbruster
2019-11-22 8:13 ` Richard Henderson
2019-11-22 8:54 ` Juan Quintela
2019-11-22 14:05 ` Markus Armbruster
2020-01-14 10:00 ` Markus Armbruster
2020-01-14 11:08 ` Juan Quintela
2020-01-20 18:43 ` Dr. David Alan Gilbert
2019-11-22 10:31 ` Philippe Mathieu-Daudé
2019-11-22 8:00 ` Markus Armbruster [this message]
2019-11-22 8:13 ` [PATCH v3 2/2] util/cutils: " Richard Henderson
2019-11-22 8:55 ` Juan Quintela
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=20191122080039.12771-3-armbru@redhat.com \
--to=armbru@redhat.com \
--cc=dgilbert@redhat.com \
--cc=i@maskray.me \
--cc=qemu-devel@nongnu.org \
--cc=quintela@redhat.com \
--cc=richard.henderson@linaro.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).