Fix incorrect int->float conversions caught by clang -Wimplicit-int-float-conversion
PATCH 1/1 Fix incorrect int->float conversions caught by clang -Wimplicit-int-float-conversion
The warning will be enabled by default in clang 10. It is not available for clang <= 9.
qemu/migration/migration.c:2038:24: error: implicit conversion from 'long' to 'double' changes value from 9223372036854775807 to 9223372036854775808 [-Werror,-Wimplicit-int-float-conversion]
...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]
--- migration/migration.c | 4 ++-- util/cutils.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)
migration/migration.c +2 -2
@@ -53,6 +53,7 @@#include "monitor/monitor.h" #include "net/announce.h" #include "qemu/queue.h" +#include <math.h> #define MAX_THROTTLE (32 << 20)/* Migration transfer speed throttling */
@@ -2035,11 +2036,10 @@ void qmp_migrate_set_downtime(double value, Error **errp) } value *=1000 ;/* Convert to milliseconds */ - value =MAX (0 ,MIN (INT64_MAX , value));MigrateSetParameters p = { .has_downtime_limit = true, - .downtime_limit = value, + .downtime_limit = (int64_t )fmin (value,nextafter (0x1p63 ,0 )), };qmp_migrate_set_parameters (&p, errp);
util/cutils.c +2 -2
@@ -239,10 +239,10 @@ static int do_strtosz(const char *nptr, const char **end, goto out; }/* -* Values >= 0xfffffffffffffc00 overflow uint64_t after their trip +* Values > nextafter(0x1p64, 0) overflow uint64_t after their trip * through double (53 bits of precision). */ -if ((val * mul >=0xfffffffffffffc00 ) || val <0 ) { +if ((val * mul >nextafter (0x1p64 ,0 )) || val <0 ) { retval = -ERANGE ; goto out; }
Signature · 1 line
2.24.0
migration/migration.c void qmp_migrate_set_downtime(double value, Error **errp)
Quoted 25 lines · Fangrui Song
The warning will be enabled by default in clang 10. It is not available for clang <= 9.
qemu/migration/migration.c:2038:24: error: implicit conversion from 'long' to 'double' changes value from 9223372036854775807 to 9223372036854775808 [-Werror,-Wimplicit-int-float-conversion]
...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]Signed-off-by Fangrui Song <[email protected]>--- migration/migration.c | 4 ++-- util/cutils.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)migration/migration.c +1
-0@@ -53,6 +53,7 @@#include "monitor/monitor.h" #include "net/announce.h" #include "qemu/queue.h" +#include <math.h> #define MAX_THROTTLE (32 << 20)/* Migration transfer speed throttling */ @@ -2035,11 +2036,10 @@ void qmp_migrate_set_downtime(double value, Error **errp)
if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) {
error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
"the range of 0 to %d seconds",
MAX_MIGRATE_DOWNTIME_SECONDS);
return;migration/migration.c void qmp_migrate_set_downtime(double value, Error **errp) · line 2036
Quoted 1 line
migration/migration.c +0
-0}
@value is now in [0,2000].
migration/migration.c void qmp_migrate_set_downtime(double value, Error **errp) · line 2038
Quoted 2 lines
migration/migration.c +0
-0value *=1000 ;/* Convert to milliseconds */
@value is in [0,2000000]
migration/migration.c void qmp_migrate_set_downtime(double value, Error **errp) · line 2038
Quoted 1 line
migration/migration.c +0
-1- value =MAX (0 ,MIN (INT64_MAX , value));
This does nothing.
migration/migration.c void qmp_migrate_set_downtime(double value, Error **errp) · line 2042
Quoted 5 lines
migration/migration.c +1
-1MigrateSetParameters p = { .has_downtime_limit = true, - .downtime_limit = value, + .downtime_limit = (int64_t )fmin (value,nextafter (0x1p63 ,0 )),
This does nothing and is hard to read :)
Can we simply drop the offending line statement instead?
Quoted 10 lines
migration/migration.c +0
-0};qmp_migrate_set_parameters (&p, errp);util/cutils.c +0
-0@@ -239,10 +239,10 @@ static int do_strtosz(const char *nptr, const char **end, goto out; }
util/cutils.c static int do_strtosz(const char *nptr, const char **end, · line 248
Quoted 10 lines
util/cutils.c +2
-2/* -* Values >= 0xfffffffffffffc00 overflow uint64_t after their trip +* Values > nextafter(0x1p64, 0) overflow uint64_t after their trip * through double (53 bits of precision). */ -if ((val * mul >=0xfffffffffffffc00 ) || val <0 ) { +if ((val * mul >nextafter (0x1p64 ,0 )) || val <0 ) { retval = -ERANGE ; goto out; }
*result = val * mul;
I figure this one is correct and hard to read.
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.
nextafter(0x1p64, 0) is a clever way to write 0xfffffffffffff800, the largest uint64_t exactly representable as double.
With your patch, val * mul in [0,0xfffffffffffff800] will be accepted.
The first val * mul above this range is 0x1p64. Rejecting it is correct, because it overflows yint64_t.
Quoted 51 lines · Markus Armbruster
migration/migration.c void qmp_migrate_set_downtime(double value, Error **errp)
Quoted 30 lines · Fangrui Song
The warning will be enabled by default in clang 10. It is not available for clang <= 9.
qemu/migration/migration.c:2038:24: error: implicit conversion from 'long' to 'double' changes value from 9223372036854775807 to 9223372036854775808 [-Werror,-Wimplicit-int-float-conversion]
...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]Signed-off-by Fangrui Song <[email protected]>--- migration/migration.c | 4 ++-- util/cutils.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)migration/migration.c +1
-0@@ -53,6 +53,7 @@#include "monitor/monitor.h" #include "net/announce.h" #include "qemu/queue.h" +#include <math.h> #define MAX_THROTTLE (32 << 20)/* Migration transfer speed throttling */ @@ -2035,11 +2036,10 @@ void qmp_migrate_set_downtime(double value, Error **errp)if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) { error_setg(errp, "Parameter 'downtime_limit' expects an integer in " "the range of 0 to %d seconds", MAX_MIGRATE_DOWNTIME_SECONDS); return;migration/migration.c void qmp_migrate_set_downtime(double value, Error **errp) · line 2036
Quoted 1 line
migration/migration.c +0
-0}@value is now in [0,2000].
migration/migration.c void qmp_migrate_set_downtime(double value, Error **errp) · line 2038
Quoted 2 lines
migration/migration.c +0
-0value *=1000 ;/* Convert to milliseconds */ @value is in [0,2000000]
migration/migration.c void qmp_migrate_set_downtime(double value, Error **errp) · line 2038
Quoted 1 line
migration/migration.c +0
-1- value =MAX (0 ,MIN (INT64_MAX , value));This does nothing.
Quoted 1 line
Quoted 8 lines
migration/migration.c void qmp_migrate_set_downtime(double value, Error **errp) · line 2042
Quoted 4 lines
MigrateSetParameters p = {
.has_downtime_limit = true,migration/migration.c +1
-1- .downtime_limit = value, + .downtime_limit = (int64_t)fmin(value, nextafter(0x1p63, 0)),This does nothing and is hard to read :)
Can we simply drop the offending line statement instead?
Agreed aboutdropping the whole bussines for migration.
Quoted 12 lines
Quoted 12 lines
}; qmp_migrate_set_parameters(&p, errp);util/cutils.c +0
-1@@ -239,10 +239,10 @@ static int do_strtosz(const char *nptr, const char **end, goto out; }/* -* Values >= 0xfffffffffffffc00 overflow uint64_t after their trip
Quoted 8 lines
util/cutils.c static int do_strtosz(const char *nptr, const char **end, · line 248
Quoted 8 lines
util/cutils.c +2
-1+* Values > nextafter(0x1p64, 0) overflow uint64_t after their trip * through double (53 bits of precision). */ -if ((val * mul >=0xfffffffffffffc00 ) || val <0 ) { +if ((val * mul >nextafter (0x1p64 ,0 )) || val <0 ) { retval = -ERANGE ; goto out; }
This comment was really bad (it says the same that the code). On the other hand, I can *kind of* understand what does 0xffff<more f's here>.
But I am at a complete loss about what value is:
nextafter(0x1p64, 0).
Can we put what value is that instead?
Thanks, Juan.
Quoted 89 lines · Juan Quintela
Quoted 51 lines · Markus Armbruster
migration/migration.c void qmp_migrate_set_downtime(double value, Error **errp)
Quoted 30 lines · Fangrui Song
The warning will be enabled by default in clang 10. It is not available for clang <= 9.
qemu/migration/migration.c:2038:24: error: implicit conversion from 'long' to 'double' changes value from 9223372036854775807 to 9223372036854775808 [-Werror,-Wimplicit-int-float-conversion]
...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]Signed-off-by Fangrui Song <[email protected]>--- migration/migration.c | 4 ++-- util/cutils.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)migration/migration.c +1
-0@@ -53,6 +53,7 @@#include "monitor/monitor.h" #include "net/announce.h" #include "qemu/queue.h" +#include <math.h> #define MAX_THROTTLE (32 << 20)/* Migration transfer speed throttling */ @@ -2035,11 +2036,10 @@ void qmp_migrate_set_downtime(double value, Error **errp)if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) { error_setg(errp, "Parameter 'downtime_limit' expects an integer in " "the range of 0 to %d seconds", MAX_MIGRATE_DOWNTIME_SECONDS); return;migration/migration.c void qmp_migrate_set_downtime(double value, Error **errp) · line 2036
Quoted 1 line
migration/migration.c +0
-0}@value is now in [0,2000].
migration/migration.c void qmp_migrate_set_downtime(double value, Error **errp) · line 2038
Quoted 2 lines
migration/migration.c +0
-0value *=1000 ;/* Convert to milliseconds */ @value is in [0,2000000]
migration/migration.c void qmp_migrate_set_downtime(double value, Error **errp) · line 2038
Quoted 1 line
migration/migration.c +0
-1- value =MAX (0 ,MIN (INT64_MAX , value));This does nothing.
Quoted 1 line
Quoted 8 lines
migration/migration.c void qmp_migrate_set_downtime(double value, Error **errp) · line 2042
Quoted 4 lines
MigrateSetParameters p = {
.has_downtime_limit = true,migration/migration.c +1
-1- .downtime_limit = value, + .downtime_limit = (int64_t)fmin(value, nextafter(0x1p63, 0)),This does nothing and is hard to read :)
Can we simply drop the offending line statement instead?
Agreed aboutdropping the whole bussines for migration.
Quoted 12 lines
Quoted 12 lines
}; qmp_migrate_set_parameters(&p, errp);util/cutils.c +0
-1@@ -239,10 +239,10 @@ static int do_strtosz(const char *nptr, const char **end, goto out; }/* -* Values >= 0xfffffffffffffc00 overflow uint64_t after their trip Quoted 8 lines
util/cutils.c static int do_strtosz(const char *nptr, const char **end, · line 248
Quoted 8 lines
util/cutils.c +2
-1+* Values > nextafter(0x1p64, 0) overflow uint64_t after their trip * through double (53 bits of precision). */ -if ((val * mul >=0xfffffffffffffc00 ) || val <0 ) { +if ((val * mul >nextafter (0x1p64 ,0 )) || val <0 ) { retval = -ERANGE ; goto out; }This comment was really bad (it says the same that the code). On the other hand, I can *kind of* understand what does 0xffff<more f's here>.
Quoted 5 lines
But I am at a complete loss about what value is:
nextafter(0x1p64, 0).Can we put what value is that instead?
It is a C99 hexadecimal floating-point literal.
0x1p64 represents hex fraction 1.0 scaled by 2**64, that is 2**64.
We can write this as val * mul > 0xfffffffffffff800p0, but I feel that counting the number of f's is error-prone and is not fun.
(We cannot use val * mul >= 0x1p64.
If FLT_EVAL_METHOD == 2, the intermediate computation val * mul will be performed at long double precision, val * mul may not by representable by a double and will overflow as (double)0x1p64.)
Quoted 103 lines · Fangrui Song
Quoted 93 lines · Juan Quintela
Quoted 54 lines · Markus Armbruster
Quoted 31 lines · Fangrui Song
The warning will be enabled by default in clang 10. It is not available for clang <= 9.
qemu/migration/migration.c:2038:24: error: implicit conversion from 'long' to 'double' changes value from 9223372036854775807 to 9223372036854775808 [-Werror,-Wimplicit-int-float-conversion]
...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]Signed-off-by Fangrui Song <[email protected]>---
migration/migration.c| 4 ++--
util/cutils.c | 4 ++--2 files changed, 4 insertions(+), 4 deletions(-)migration/migration.c +1
-0@@ -53,6 +53,7 @@ #include "monitor/monitor.h" #include "net/announce.h" #include "qemu/queue.h" +#include <math.h> #define MAX_THROTTLE (32 << 20) /* Migration transfer speedthrottling */
migration/migration.c void qmp_migrate_set_downtime(double value, Error · line 2036
Quoted 1 line
migration/migration.c +0
-0@@ -2035,11 +2036,10 @@ void qmp_migrate_set_downtime(double value, Error **errp)if (value < 0 || value >
MAX_MIGRATE_DOWNTIME_SECONDS) { error_setg(errp, "Parameter 'downtime_limit' expects an integer in "
"the range of 0 to %d seconds",MAX_MIGRATE_DOWNTIME_SECONDS); return;migration/migration.c void qmp_migrate_set_downtime(double value, Error · line 2037
Quoted 1 line
migration/migration.c +0
-0}@value is now in [0,2000].
Quoted 2 lines
value *= 1000; /* Convert to milliseconds */
@value is in [0,2000000]
Quoted 1 line
- value =
MAX(0, MIN(INT64_MAX, value));This does nothing.
Quoted 1 line
Quoted 8 lines
Quoted 4 lines
MigrateSetParameters p = {
.has_downtime_limit = true,
- .downtime_limit = value,
+ .downtime_limit = (int64_t)fmin(value,nextafter(0x1p63, 0)),This does nothing and is hard to read :)
Can we simply drop the offending line statement instead?
Agreed aboutdropping the whole bussines for migration.
Quoted 13 lines
Quoted 13 lines
};
qmp_migrate_set_parameters(&p, errp);util/cutils.c +0
-1@@ -239,10 +239,10 @@ static int do_strtosz(const char *nptr, const char **end, goto out; } /* -* Values >= 0xfffffffffffffc00 overflow uint64_t after their trip Quoted 8 lines
Quoted 8 lines
util/cutils.c +2
-1+* Values > nextafter(0x1p64, 0) overflow uint64_t after their trip * through double (53 bits of precision). */ - if ((val * mul >= 0xfffffffffffffc00) || val < 0) { + if ((val * mul > nextafter(0x1p64, 0)) || val < 0) { retval = -ERANGE; goto out;}
This comment was really bad (it says the same that the code). On the other hand, I can *kind of* understand what does 0xffff<more f's here>.
Quoted 5 lines
But I am at a complete loss about what value is:
nextafter(0x1p64, 0).Can we put what value is that instead?
It is a C99 hexadecimal floating-point literal.
0x1p64 represents hex fraction 1.0 scaled by 2**64, that is 2**64.
Quoted 7 lines
We can write this as
val * mul > 0xfffffffffffff800p0, but I feel that counting the number of f's is error-prone and is not fun.(We cannot use val * mul >= 0x1p64.
IfFLT_EVAL_METHOD== 2, the intermediate computation val * mul will be performed at long double precision, val * mul may not by representable by a double and will overflow as (double)0x1p64.)
I agree about not spelling out the f's, or the 0x800 at the end. That's something that the compiler can do for us, resolving this standard library function at compile-time.
We just need a better comment. Perhaps:
/*
* 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".
*/
r~
Quoted 4 lines · Richard Henderson
Quoted 3 lines · Fangrui Song
Quoted 2 lines · Juan Quintela
Quoted 1 line · Markus Armbruster
Fangrui Song <[email protected]> writes:
[...]
Quoted 44 lines
Quoted 32 lines
Quoted 23 lines
Quoted 10 lines
Quoted 10 lines
util/cutils.c +0
-1@@ -239,10 +239,10 @@ static int do_strtosz(const char *nptr, const char **end, goto out; } /* -* Values >= 0xfffffffffffffc00 overflow uint64_t after their trip Quoted 8 lines
Quoted 8 lines
util/cutils.c +2
-1+* Values > nextafter(0x1p64, 0) overflow uint64_t after their trip * through double (53 bits of precision). */ - if ((val * mul >= 0xfffffffffffffc00) || val < 0) { + if ((val * mul > nextafter(0x1p64, 0)) || val < 0) { retval = -ERANGE; goto out;}
This comment was really bad (it says the same that the code). On the other hand, I can *kind of* understand what does 0xffff<more f's here>.
Quoted 5 lines
But I am at a complete loss about what value is:
nextafter(0x1p64, 0).Can we put what value is that instead?
It is a C99 hexadecimal floating-point literal.
0x1p64 represents hex fraction 1.0 scaled by 2**64, that is 2**64.Quoted 7 lines
We can write this as
val * mul > 0xfffffffffffff800p0, but I feel that counting the number of f's is error-prone and is not fun.(We cannot use val * mul >= 0x1p64.
IfFLT_EVAL_METHOD== 2, the intermediate computation val * mul will be performed at long double precision, val * mul may not by representable by a double and will overflow as (double)0x1p64.)I agree about not spelling out the f's, or the
0x800at the end. That's something that the compiler can do for us, resolving this standard library function at compile-time.
Quoted 8 lines
We just need a better comment. Perhaps:
/*
* Values nearUINT64_MAXoverflow 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".
*/
Yes, please.
Quoted 53 lines · Markus Armbruster
Quoted 4 lines · Richard Henderson
Quoted 3 lines · Fangrui Song
Quoted 2 lines · Juan Quintela
Quoted 1 line · Markus Armbruster
Fangrui Song <[email protected]> writes:
[...]
Quoted 46 lines
Quoted 32 lines
Quoted 23 lines
Quoted 10 lines
Quoted 10 lines
util/cutils.c +0
-1@@ -239,10 +239,10 @@ static int do_strtosz(const char *nptr, const char **end, goto out; } /* -* Values >= 0xfffffffffffffc00 overflow uint64_t after their trip Quoted 8 lines
Quoted 8 lines
util/cutils.c +2
-1+* Values > nextafter(0x1p64, 0) overflow uint64_t after their trip * through double (53 bits of precision). */ - if ((val * mul >= 0xfffffffffffffc00) || val < 0) { + if ((val * mul > nextafter(0x1p64, 0)) || val < 0) { retval = -ERANGE; goto out;}
This comment was really bad (it says the same that the code). On the other hand, I can *kind of* understand what does 0xffff<more f's here>.
Quoted 5 lines
But I am at a complete loss about what value is:
nextafter(0x1p64, 0).Can we put what value is that instead?
It is a C99 hexadecimal floating-point literal.
0x1p64 represents hex fraction 1.0 scaled by 2**64, that is 2**64.Quoted 7 lines
We can write this as
val * mul > 0xfffffffffffff800p0, but I feel that counting the number of f's is error-prone and is not fun.(We cannot use val * mul >= 0x1p64.
IfFLT_EVAL_METHOD== 2, the intermediate computation val * mul will be performed at long double precision, val * mul may not by representable by a double and will overflow as (double)0x1p64.)I agree about not spelling out the f's, or the
0x800at the end. That's something that the compiler can do for us, resolving this standard library function at compile-time.We just need a better comment. Perhaps:
Quoted 8 lines
Quoted 6 lines
/*
* Values nearUINT64_MAXoverflow 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".
*/Yes, please.
Thanks for the suggestion. Attached a new patch.
From 13312e91e5565a6bed8c394d5711603c7a8f8a3c Mon Sep 17 00:00:00 2001
From: Fangrui Song <[email protected]>
Date: Fri, 15 Nov 2019 16:27:47 -0800
Subject: [PATCH] Fix incorrect integer->float conversion caught by clang -Wimplicit-int-float-conversion
To: [email protected]
The warning will be enabled by default in clang 10. It is not available for clang <= 9.
qemu/migration/migration.c:2038:24: error: implicit conversion from 'long' to 'double' changes value from 9223372036854775807 to 9223372036854775808 [-Werror,-Wimplicit-int-float-conversion]
...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]
--- migration/migration.c | 3 +-- util/cutils.c | 8 +++++--- 2 files changed, 6 insertions(+), 5 deletions(-)
migration/migration.c +1 -2
@@ -2035,11 +2035,10 @@ void qmp_migrate_set_downtime(double value, Error **errp) } value *=1000 ;/* Convert to milliseconds */ - value =MAX (0 ,MIN (INT64_MAX , value));MigrateSetParameters p = { .has_downtime_limit = true, - .downtime_limit = value, + .downtime_limit = (int64_t )value, };qmp_migrate_set_parameters (&p, errp);
util/cutils.c +5 -3
@@ -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; }
Signature · 1 line
2.24.0
Quoted 49 lines
Quoted 26 lines · Fangrui Song
The warning will be enabled by default in clang 10. It is not available for clang <= 9.
qemu/migration/migration.c:2038:24: error: implicit conversion from 'long' to 'double' changes value from 9223372036854775807 to 9223372036854775808 [-Werror,-Wimplicit-int-float-conversion]
...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]Signed-off-by Fangrui Song <address@hidden>--- migration/migration.c | 4 ++-- util/cutils.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)migration/migration.c +1
-0@@ -53,6 +53,7 @@#include "monitor/monitor.h" #include "net/announce.h" #include "qemu/queue.h" +#include <math.h> #define MAX_THROTTLE (32 << 20)/* Migration transfer speed throttling */
migration/migration.c void qmp_migrate_set_downtime(double value, Error · line 2036
Quoted 1 line
migration/migration.c +0
-0@@ -2035,11 +2036,10 @@ void qmp_migrate_set_downtime(double value, Error **errp)if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) { error_setg(errp, "Parameter 'downtime_limit' expects an integer in " "the range of 0 to %d seconds", MAX_MIGRATE_DOWNTIME_SECONDS); return;migration/migration.c void qmp_migrate_set_downtime(double value, Error · line 2037
Quoted 1 line
migration/migration.c +0
-0}@value is now in [0,2000].
migration/migration.c void qmp_migrate_set_downtime(double value, Error · line 2038
Quoted 2 lines
migration/migration.c +0
-0value *=1000 ;/* Convert to milliseconds */ @value is in [0,2000000]
migration/migration.c void qmp_migrate_set_downtime(double value, Error · line 2038
Quoted 1 line
migration/migration.c +0
-1- value =MAX (0 ,MIN (INT64_MAX , value));This does nothing.
Quoted 1 line
Quoted 8 lines
migration/migration.c
Quoted 4 lines
MigrateSetParameters p = {
.has_downtime_limit = true,migration/migration.c +1
-1- .downtime_limit = value, + .downtime_limit = (int64_t)fmin(value, nextafter(0x1p63, 0)),This does nothing and is hard to read :)
Can we simply drop the offending line statement instead?
Fixed in the new patch.
Quoted 29 lines
Quoted 20 lines
}; qmp_migrate_set_parameters(&p, errp);util/cutils.c +2
-2@@ -239,10 +239,10 @@ static int do_strtosz(const char *nptr, const char **end, goto out; }/* -* Values >= 0xfffffffffffffc00 overflow uint64_t after their trip +* Values > nextafter(0x1p64, 0) overflow uint64_t after their trip * through double (53 bits of precision). */ -if ((val * mul >=0xfffffffffffffc00 ) || val <0 ) { +if ((val * mul >nextafter (0x1p64 ,0 )) || val <0 ) { retval = -ERANGE ; goto out;Quoted 1 line
}*result = val * mul;I figure this one is correct and hard to read.
0xfffffffffffffc00is not representable exactly as double. It's half-way between the representable values0xfffffffffffff800and0x10000000000000000. Which one we get is implementation-defined. Bad.
Quoted 7 lines
nextafter(0x1p64, 0)is a clever way to write0xfffffffffffff800, the largestuint64_texactly representable as double.With your patch, val * mul in [0,0xfffffffffffff800] will be accepted.
The first val * mul above this range is 0x1p64. Rejecting it is correct, because it overflows
yint64_t.
I am not subscribed, so apologize that this email may be off the thread.
(The binutils mailing list allows a user to download the raw email so I can still reply to a specific email, but this list does not provide such feature.)
From 5f1c5a42794ddcbabb63d9af920d9f437ea90a9f Mon Sep 17 00:00:00 2001
From: Fangrui Song <[email protected]>
Date: Fri, 15 Nov 2019 16:27:47 -0800
Subject: [PATCH] Fix incorrect integer->float conversions caught by clang
-Wimplicit-int-float-conversion
To: [email protected]
The warning will be enabled by default in clang 10. It is not available for clang <= 9.
qemu/migration/migration.c:2038:24: error: implicit conversion from 'long' to 'double' changes value from 9223372036854775807 to 9223372036854775808 [-Werror,-Wimplicit-int-float-conversion]
...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]
--- migration/migration.c | 3 +-- util/cutils.c | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-)
migration/migration.c +1 -2
@@ -2035,11 +2035,10 @@ void qmp_migrate_set_downtime(double value, Error **errp) } value *=1000 ;/* Convert to milliseconds */ - value =MAX (0 ,MIN (INT64_MAX , value));MigrateSetParameters p = { .has_downtime_limit = true, - .downtime_limit = value, + .downtime_limit = (int64_t )value, };qmp_migrate_set_parameters (&p, errp);
util/cutils.c +2 -2
@@ -239,10 +239,10 @@ static int do_strtosz(const char *nptr, const char **end, goto out; }/* -* Values >= 0xfffffffffffffc00 overflow uint64_t after their trip +* Values > nextafter(0x1p64, 0) overflow uint64_t after their trip * through double (53 bits of precision). */ -if ((val * mul >=0xfffffffffffffc00 ) || val <0 ) { +if ((val * mul >nextafter (0x1p64 ,0 )) || val <0 ) { retval = -ERANGE ; goto out; }
Signature · 1 line
2.24.0
Quoted 6 lines · Fangrui Song
Quoted 2 lines
Can we simply drop the offending line statement instead?
Fixed in the new patch.
Quoted 8 lines
Quoted 2 lines
The first val * mul above this range is 0x1p64. Rejecting it is correct, because it overflows
yint64_t.I am not subscribed, so apologize that this email may be off the thread.
(The binutils mailing list allows a user to download the raw email so I can still reply to a specific email, but this list does not provide such feature.)
Actually, it's better to post a v2 patch as a new top-level thread, rather than buried as an attachment to a reply to v1, because our CI tooling doesn't see through the attachment (nor was it easy for me to reply to the v2 patch - I had to open the attachment to paste its text inline below...).
More patch submission hints at https://wiki.qemu.org/Contribute/SubmitAPatch
Quoted 12 lines
Quoted 1 line
From
5f1c5a42794ddcbabb63d9af920d9f437ea90a9fMon Sep 17 00:00:00 2001From: Fangrui Song <[email protected]>
Date: Fri, 15 Nov 2019 16:27:47 -0800
Subject: [PATCH] Fix incorrectinteger->floatconversions caught by clang
-Wimplicit-int-float-conversion
To: [email protected]The warning will be enabled by default in clang 10. It is not available for clang <= 9.
+++
b/migration/migration.c
migration/migration.c void qmp_migrate_set_downtime(double value, Error **errp) · line 2042
Quoted 10 lines
migration/migration.c +1
-2@@ -2035,11 +2035,10 @@ void qmp_migrate_set_downtime(double value, Error **errp) } value *=1000 ;/* Convert to milliseconds */ - value =MAX (0 ,MIN (INT64_MAX , value));MigrateSetParameters p = { .has_downtime_limit = true, - .downtime_limit = value, + .downtime_limit = (int64_t )value, };
The explicit cast looks odd without a comment (generally, we try to avoid casts, so a comment such as /* explicit cast to silence compiler */ can be useful)
util/cutils.c static int do_strtosz(const char *nptr, const char **end, · line 243
Quoted 13 lines
migration/migration.c +0
-0qmp_migrate_set_parameters (&p, errp);util/cutils.c +1
-1@@ -239,10 +239,10 @@ static int do_strtosz(const char *nptr, const char **end, goto out; }/* -* Values >= 0xfffffffffffffc00 overflow uint64_t after their trip +* Values > nextafter(0x1p64, 0) overflow uint64_t after their trip * through double (53 bits of precision).
I thought we agreed on more text than just this (in particular, that the nextafter() call represents 2^64 rounded towards zero).
Quoted 8 lines
util/cutils.c +1
-1*/ -if ((val * mul >=0xfffffffffffffc00 ) || val <0 ) { +if ((val * mul >nextafter (0x1p64 ,0 )) || val <0 ) { retval = -ERANGE ; goto out; }Signature · 1 line
2.24.0
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org
PATCH v2 1/1 Fix incorrect integer->float conversions caught by clang -Wimplicit-int-float-conversion
Quoted 13 lines · Eric Blake
Quoted 11 lines · Fangrui Song
Quoted 2 lines
Can we simply drop the offending line statement instead?
Fixed in the new patch.
Quoted 2 lines
The first val * mul above this range is 0x1p64. Rejecting it is correct, because it overflows
yint64_t.I am not subscribed, so apologize that this email may be off the thread.
Quoted 7 lines
Quoted 3 lines
(The binutils mailing list allows a user to download the raw email so I can still reply to a specific email, but this list does not provide such feature.)
Actually, it's better to post a v2 patch as a new top-level thread, rather than buried as an attachment to a reply to v1, because our CI tooling doesn't see through the attachment (nor was it easy for me to reply to the v2 patch - I had to open the attachment to paste its text inline below...).
More patch submission hints at https://wiki.qemu.org/Contribute/SubmitAPatch
Retitled to [PATCH v2]
Quoted 10 lines
Quoted 9 lines
Quoted 1 line
From
5f1c5a42794ddcbabb63d9af920d9f437ea90a9fMon Sep 17 00:00:00 2001From: Fangrui Song <[email protected]>
Date: Fri, 15 Nov 2019 16:27:47 -0800
Subject: [PATCH] Fix incorrectinteger->floatconversions caught by clang -Wimplicit-int-float-conversion
To: [email protected]The warning will be enabled by default in clang 10. It is not available for clang <= 9.
Quoted 13 lines
Review void qmp_migrate_set_downtime(double value, Error **errp) · line 2040
Quoted 10 lines
+++
b/migration/migration.c(quoted diff) +1
-2@@ -2035,11 +2035,10 @@ void qmp_migrate_set_downtime(double value, Error **errp) } value *= 1000; /* Convert to milliseconds */ - value = MAX(0, MIN(INT64_MAX, value)); MigrateSetParameters p = { .has_downtime_limit = true, - .downtime_limit = value, + .downtime_limit = (int64_t)value, };The explicit cast looks odd without a comment (generally, we try to avoid casts, so a comment such as /* explicit cast to silence compiler */ can be useful)
downtime_limit is an int64_t while value is a double.
There is a diagnostic (-Wfloat-conversion, included by -Wconversion)
warning: conversion from ‘double’ to ‘int64_t’ {aka ‘long int’} may change value [-Wfloat-conversion]
but it is not enabled by -Wall or -Wextra.
I am not familiar with qemu coding style, but I strongly feel it is a good thing to add an explicit cast. If it does not fit the style, I hope a maintainer can delete that for me.
Quoted 6 lines
Quoted 6 lines
(quoted diff) +0
-0qmp_migrate_set_parameters(&p, errp);util/cutils.c +0
-0@@ -239,10 +239,10 @@ static int do_strtosz(const char *nptr, const char **end,
Quoted 15 lines
util/cutils.c static int do_strtosz(const char *nptr, const char **end, · line 243
Quoted 6 lines
util/cutils.c +1
-1goto out; }/* -* Values >= 0xfffffffffffffc00 overflow uint64_t after their trip +* Values > nextafter(0x1p64, 0) overflow uint64_t after their trip * through double (53 bits of precision). I thought we agreed on more text than just this (in particular, that the
nextafter()call represents 2^64 rounded towards zero).Quoted 6 lines
util/cutils.c +1
-1*/ -if ((val * mul >=0xfffffffffffffc00 ) || val <0 ) { +if ((val * mul >nextafter (0x1p64 ,0 )) || val <0 ) { retval = -ERANGE ; goto out; }
Sorry, I uploaded the wrong patch file. Attaching the correct one now.
From d533585df39083e88adc50b881a4be74125c837e Mon Sep 17 00:00:00 2001
From: Fangrui Song <[email protected]>
Date: Fri, 15 Nov 2019 16:27:47 -0800
Subject: [PATCH] Fix incorrect integer->float conversion caught by clang
-Wimplicit-int-float-conversion
To: [email protected]
The warning will be enabled by default in clang 10. It is not available for clang <= 9.
qemu/migration/migration.c:2038:24: error: implicit conversion from 'long' to 'double' changes value from 9223372036854775807 to 9223372036854775808 [-Werror,-Wimplicit-int-float-conversion]
...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]
--- migration/migration.c | 3 +-- util/cutils.c | 8 +++++--- 2 files changed, 6 insertions(+), 5 deletions(-)
migration/migration.c +1 -2
@@ -2035,11 +2035,10 @@ void qmp_migrate_set_downtime(double value, Error **errp) } value *=1000 ;/* Convert to milliseconds */ - value =MAX (0 ,MIN (INT64_MAX , value));MigrateSetParameters p = { .has_downtime_limit = true, - .downtime_limit = value, + .downtime_limit = (int64_t )value, };qmp_migrate_set_parameters (&p, errp);
util/cutils.c +5 -3
@@ -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; }
Signature · 1 line
2.24.0
Re: [PATCH v2] Fix incorrect integer->float conversions caught by clang -Wimplicit-int-float-conversion
Quoted 9 lines · Fangrui Song
Quoted 8 lines · Eric Blake
Quoted 5 lines · Fangrui Song
Quoted 2 lines
Can we simply drop the offending line statement instead?
Fixed in the new patch.
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines
The first val * mul above this range is 0x1p64. Rejecting it is correct, because it overflows
yint64_t.I am not subscribed, so apologize that this email may be off the thread.
(The binutils mailing list allows a user to download the raw email so I can still reply to a specific email, but this list does not provide such feature.)
There's <https://lists.gnu.org/archive/mbox/qemu-devel/>.
Quoted 9 lines
Quoted 7 lines
Actually, it's better to post a v2 patch as a new top-level thread, rather than buried as an attachment to a reply to v1, because our CI tooling doesn't see through the attachment (nor was it easy for me to reply to the v2 patch - I had to open the attachment to paste its text inline below...).
More patch submission hints at https://wiki.qemu.org/Contribute/SubmitAPatch
Retitled to [PATCH v2]
Good, such versioning is essential to avoid confusion. Next time, start a new top-level thread for v2. Our patch submission processing (automated as well as human) expects that.
I just did it for you: "[PATCH v3 0/2] Fix incorrect integer->float conversion caught by clang".
Many thanks for your fixes!