]> git.ipfire.org Git - thirdparty/qemu.git/commit
util/cutils: Fix incorrect integer->float conversion caught by clang
authorFangrui Song <i@maskray.me>
Fri, 22 Nov 2019 08:00:39 +0000 (09:00 +0100)
committerMarkus Armbruster <armbru@redhat.com>
Mon, 25 Nov 2019 05:00:05 +0000 (06:00 +0100)
commit25f74087c695364dfaa87443b1040a3aa5c29008
treeb887c91b7bf15b6bef1e614004c9c0b078347b41
parent2061735ff09f9d5e67c501a96227b470e7de69b1
util/cutils: Fix incorrect integer->float conversion caught by clang

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>
Message-Id: <20191122080039.12771-3-armbru@redhat.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Reviewed-by: Juan Quintela <quintela@redhat.com>
util/cutils.c