misc: fix Clang10 warnings
cover misc: fix Clang10 warnings
Fix 2 warnings when building with Clang on Fedora32.
Philippe Mathieu-Daudé (2):
audio/mixeng: Fix Clang 'int-conversion' warning linux-user/mmap: Fix Clang 'type-limit-compare' warning
audio/mixeng.c | 2 +- linux-user/mmap.c | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-)
Signature · 1 line
2.21.3
RFC PATCH 1/2 audio/mixeng: Fix Clang 'int-conversion' warning
When building with Clang 10 on Fedora 32, we get:
CC audio/mixeng.o
audio/mixeng.c:274:34: error: implicit conversion from 'unsigned int' to 'float' changes value from 4294967295 to 4294967296 [-Werror,-Wimplicit-int-float-conversion]
static const float float_scale = UINT_MAX / 2.f;
^~~~~~~~ ~
/usr/lib64/clang/10.0.0/include/limits.h:56:37: note: expanded from macro 'UINT_MAX'
#define UINT_MAX (__INT_MAX__ *2U +1U)
~~~~~~~~~~~~~~~~~^~~Fix by using a 64-bit float for the conversion, before casting back to 32-bit float.
--- audio/mixeng.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
audio/mixeng.c +1 -1
@@ -271,7 +271,7 @@ f_sample *mixeng_clip[2][2][2][3] = {#define CONV_NATURAL_FLOAT (x) (x)#define CLIP_NATURAL_FLOAT (x) (x)#else -static const float float_scale =UINT_MAX /2.f ; +static const float float_scale =UINT_MAX /2. ;#define CONV_NATURAL_FLOAT (x) ((x) * float_scale)#ifdef RECIPROCAL
Signature · 1 line
2.21.3
Re: [RFC PATCH 1/2] audio/mixeng: Fix Clang 'int-conversion' warning
Quoted 23 lines · Philippe Mathieu-Daudé
When building with Clang 10 on Fedora 32, we get:
CC
audio/mixeng.o
audio/mixeng.c:274:34: error: implicit conversion from 'unsigned int' to 'float' changes value from 4294967295 to 4294967296 [-Werror,-Wimplicit-int-float-conversion]static const float float_scale = UINT_MAX / 2.f; ^~~~~~~~ ~ /usr/lib64/clang/10.0.0/include/limits.h:56:37: note: expanded from macro 'UINT_MAX' #define UINT_MAX (__INT_MAX__ *2U +1U) ~~~~~~~~~~~~~~~~~^~~Fix by using a 64-bit float for the conversion, before casting back to 32-bit float.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>---
audio/mixeng.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)audio/mixeng.c +0
-0
audio/mixeng.c f_sample *mixeng_clip[2][2][2][3] = { · line 274
Quoted 5 lines
audio/mixeng.c +1
-1@@ -271,7 +271,7 @@ f_sample *mixeng_clip[2][2][2][3] = {#define CONV_NATURAL_FLOAT (x) (x)#define CLIP_NATURAL_FLOAT (x) (x)#else -static const float float_scale =UINT_MAX /2.f ; +static const float float_scale =UINT_MAX /2. ;
Maybe writing it as 2.0 is easier to read and looks nicer.
Regards,
BALATON Zoltan
Re: [RFC PATCH 1/2] audio/mixeng: Fix Clang 'int-conversion' warning
In reply to Philippe Mathieu-DaudéQuoted 10 lines · Philippe Mathieu-Daudé
When building with Clang 10 on Fedora 32, we get:
CC
audio/mixeng.o
audio/mixeng.c:274:34: error: implicit conversion from 'unsigned int' to 'float' changes value from 4294967295 to 4294967296 [-Werror,-Wimplicit-int-float-conversion]static const float float_scale = UINT_MAX / 2.f; ^~~~~~~~ ~ /usr/lib64/clang/10.0.0/include/limits.h:56:37: note: expanded from macro 'UINT_MAX' #define UINT_MAX (__INT_MAX__ *2U +1U) ~~~~~~~~~~~~~~~~~^~~
Quoted 7 lines
Fix by using a 64-bit float for the conversion, before casting back to 32-bit float.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- audio/mixeng.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
No, this should be fixed properly.
First, the warning is in the !FLOAT_MIXENG branch. IMO that means we should not be using floating point at all, and this should be a simple integral multiply/shift.
I had a brief look at this before the 5.0 release. The arithmetic all through audio looks confused to me. There's a combination of shifting and masking (implying a scale by 1<<32), and multiplication and division by UINT32_MAX.
I'm reasonably certain that every appearance of UINT32_MAX in this code is an off-by-one bug, or a misuse of the constant.
r~
Re: [RFC PATCH 1/2] audio/mixeng: Fix Clang 'int-conversion' warning
In reply to Philippe Mathieu-DaudéQuoted 14 lines
Fix by using a 64-bit float for the conversion, before casting back to 32-bit float.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- audio/mixeng.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)audio/mixeng.c +0
-0
audio/mixeng.c f_sample *mixeng_clip[2][2][2][3] = { · line 274
Quoted 5 lines
audio/mixeng.c +1
-1@@ -271,7 +271,7 @@ f_sample *mixeng_clip[2][2][2][3] = {#define CONV_NATURAL_FLOAT (x) (x)#define CLIP_NATURAL_FLOAT (x) (x)#else -static const float float_scale =UINT_MAX /2.f ; +static const float float_scale =UINT_MAX /2. ;
I would prefer an explicit cast of UINT_MAX to float. This is what we already have in audio/mixeng_template.h in the conf_* and clip_* functions with FLOAT_MIXENG defined. I think similar functions should look similar.
audio/mixeng.c f_sample *mixeng_clip[2][2][2][3] = { · line 277
Quoted 3 lines
audio/mixeng.c +0
-0#define CONV_NATURAL_FLOAT (x) ((x) * float_scale)#ifdef RECIPROCAL
Please don't forget to fix the RECIPROCAL case.
Btw. the problem was reported here:
https://lists.nongnu.org/archive/html/qemu-devel/2020-03/msg02270.html
With best regards,
Volker
Re: [RFC PATCH 1/2] audio/mixeng: Fix Clang 'int-conversion' warning
Quoted 24 lines · Volker Rümelin
Quoted 14 lines
Fix by using a 64-bit float for the conversion, before casting back to 32-bit float.
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- audio/mixeng.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)audio/mixeng.c +0
-0audio/mixeng.c f_sample *mixeng_clip[2][2][2][3] = { · line 274
Quoted 5 lines
audio/mixeng.c +1
-1@@ -271,7 +271,7 @@ f_sample *mixeng_clip[2][2][2][3] = {#define CONV_NATURAL_FLOAT (x) (x)#define CLIP_NATURAL_FLOAT (x) (x)#else -static const float float_scale =UINT_MAX /2.f ; +static const float float_scale =UINT_MAX /2. ;I would prefer an explicit cast of
UINT_MAXto float. This is what we already have inaudio/mixeng_template.hin the conf_* and clip_* functions withFLOAT_MIXENGdefined. I think similar functions should look similar.Quoted 2 lines
audio/mixeng.c +0
-0#define CONV_NATURAL_FLOAT (x) ((x) * float_scale)
Quoted 7 lines
audio/mixeng.c f_sample *mixeng_clip[2][2][2][3] = { · line 277
Quoted 1 line
audio/mixeng.c +0
-0#ifdef RECIPROCAL Please don't forget to fix the RECIPROCAL case.
Btw. the problem was reported here:
https://lists.nongnu.org/archive/html/qemu-devel/2020-03/msg02270.html
Ah I missed that, I now feel safer knowing developers who understand this code are already trying to fix it, thanks Volker!
Quoted 5 lines
With best regards,
Volker
RFC PATCH 2/2 linux-user/mmap: Fix Clang 'type-limit-compare' warning
In reply to Philippe Mathieu-DaudéWhen building with Clang 10 on Fedora 32, we get:
CC linux-user/mmap.o
linux-user/mmap.c:720:49: error: result of comparison 'unsigned long' > 18446744073709551615 is always false [-Werror,-Wtautological-type-limit-compare]
if ((unsigned long)host_addr + new_size > (abi_ulong)-1) {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~Fix by restricting the check for when target sizeof(abi_ulong) is smaller than target sizeof(unsigned long).
--- linux-user/mmap.c | 2 ++ 1 file changed, 2 insertions(+)
linux-user/mmap.c +2 -0
@@ -714,6 +714,7 @@ abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, errno =ENOMEM ; host_addr =MAP_FAILED ; } +#if TARGET_ABI_BITS <TARGET_LONG_BITS /* Check if address fits target address space */ if ((unsigned long )host_addr + new_size > (abi_ulong)-1 ) {/* Revert mremap() changes */
@@ -721,6 +722,7 @@ abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, errno =ENOMEM ; host_addr = MAP_FAILED; } +#endif /* TARGET_ABI_BITS < TARGET_LONG_BITS */ }if (host_addr ==MAP_FAILED ) {
Signature · 1 line
2.21.3
Re: [RFC PATCH 2/2] linux-user/mmap: Fix Clang 'type-limit-compare' warning
нед, 3. мај 2020. у 13:33 Philippe Mathieu-Daudé <[email protected]> је написао/ла:
Quoted 29 lines
When building with Clang 10 on Fedora 32, we get:
CC
linux-user/mmap.o
linux-user/mmap.c:720:49: error: result of comparison 'unsigned long' > 18446744073709551615 is always false [-Werror,-Wtautological-type-limit-compare]if ((unsigned long)host_addr + new_size > (abi_ulong)-1) { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~Fix by restricting the check for when target
sizeof(abi_ulong)is smaller than target sizeof(unsigned long).Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- linux-user/mmap.c | 2 ++ 1 file changed, 2 insertions(+)linux-user/mmap.c +1
-0@@ -714,6 +714,7 @@ abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, errno =ENOMEM ; host_addr =MAP_FAILED ; } +#if TARGET_ABI_BITS <TARGET_LONG_BITS /* Check if address fits target address space */ if ((unsigned long )host_addr + new_size > (abi_ulong)-1 ) {/* Revert mremap() changes */
linux-user/mmap.c abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, · line 725
Quoted 4 lines
linux-user/mmap.c +1
-0@@ -721,6 +722,7 @@ abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, errno =ENOMEM ; host_addr = MAP_FAILED; } +#endif /* TARGET_ABI_BITS < TARGET_LONG_BITS */
Hm, Philippe, this will silence the clang error, but is this the right thing to do?
Why do you think the case:
TARGET_ABI_BITS < TARGET_LONG_BITS
doesn't need this check? In any case, for clarity, the reason should be mentioned in the commit message.
Regards,
Aleksandar
Quoted 7 lines
linux-user/mmap.c +0
-1}if (host_addr ==MAP_FAILED ) { --2.21.3
Re: [RFC PATCH 2/2] linux-user/mmap: Fix Clang 'type-limit-compare' warning
нед, 3. мај 2020. у 14:49 Aleksandar Markovic
<[email protected]> је написао/ла:
Quoted 19 lines
нед, 3. мај 2020. у 13:33 Philippe Mathieu-Daudé <[email protected]> је написао/ла:
Quoted 16 lines
When building with Clang 10 on Fedora 32, we get:
CC
linux-user/mmap.o
linux-user/mmap.c:720:49: error: result of comparison 'unsigned long' > 18446744073709551615 is always false [-Werror,-Wtautological-type-limit-compare]if ((unsigned long)host_addr + new_size > (abi_ulong)-1) { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~Fix by restricting the check for when target
sizeof(abi_ulong)is smaller than target sizeof(unsigned long).Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- linux-user/mmap.c | 2 ++ 1 file changed, 2 insertions(+)
Quoted 9 lines
linux-user/mmap.c abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, · line 717
Quoted 9 lines
linux-user/mmap.c +1
-0@@ -714,6 +714,7 @@ abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, errno =ENOMEM ; host_addr =MAP_FAILED ; } +#if TARGET_ABI_BITS <TARGET_LONG_BITS
Or, for that matter, a comment should be inserted before this line with explanation why the check is not needed for this case.
I think QEMU is too full with unexplained "ifdefs", which, of course, doesn't help readibility.
Quoted 30 lines
linux-user/mmap.c abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, · line 725
Quoted 8 lines
linux-user/mmap.c +1
-0/* Check if address fits target address space */ if ((unsigned long )host_addr + new_size > (abi_ulong)-1 ) {/* Revert mremap() changes */ @@ -721,6 +722,7 @@ abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, errno =ENOMEM ; host_addr = MAP_FAILED; } +#endif /* TARGET_ABI_BITS < TARGET_LONG_BITS */ Hm, Philippe, this will silence the clang error, but is this the right thing to do?
Why do you think the case:
TARGET_ABI_BITS<TARGET_LONG_BITSdoesn't need this check? In any case, for clarity, the reason should be mentioned in the commit message.
Regards,
AleksandarQuoted 7 lines
linux-user/mmap.c +0
-1}if (host_addr ==MAP_FAILED ) { --2.21.3
Re: [RFC PATCH 2/2] linux-user/mmap: Fix Clang 'type-limit-compare' warning
In reply to Aleksandar MarkovicQuoted 39 lines · Aleksandar Markovic
нед, 3. мај 2020. у 13:33 Philippe Mathieu-Daudé <[email protected]> је написао/ла:
Quoted 29 lines
When building with Clang 10 on Fedora 32, we get:
CC
linux-user/mmap.o
linux-user/mmap.c:720:49: error: result of comparison 'unsigned long' > 18446744073709551615 is always false [-Werror,-Wtautological-type-limit-compare]if ((unsigned long)host_addr + new_size > (abi_ulong)-1) { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~Fix by restricting the check for when target
sizeof(abi_ulong)is smaller than target sizeof(unsigned long).Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- linux-user/mmap.c | 2 ++ 1 file changed, 2 insertions(+)linux-user/mmap.c +1
-0@@ -714,6 +714,7 @@ abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, errno =ENOMEM ; host_addr =MAP_FAILED ; } +#if TARGET_ABI_BITS <TARGET_LONG_BITS /* Check if address fits target address space */ if ((unsigned long )host_addr + new_size > (abi_ulong)-1 ) {/* Revert mremap() changes */ linux-user/mmap.c abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, · line 725
Quoted 4 lines
linux-user/mmap.c +1
-0@@ -721,6 +722,7 @@ abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, errno =ENOMEM ; host_addr = MAP_FAILED; } +#endif /* TARGET_ABI_BITS < TARGET_LONG_BITS */ Hm, Philippe, this will silence the clang error, but is this the right thing to do?
Quoted 5 lines
Why do you think the case:
TARGET_ABI_BITS<TARGET_LONG_BITSdoesn't need this check?
I think that's quite obvious from the clang warning -- the test is always false due to type limits.
That said, this is at minimum the second occurrence of having to add ifdefs to work around this particular new warning, because there does not seem to be any other way to suppress the warning, and I'm not keen on that.
I would prefer that we disable the warning on the compiler command line in configure.
r~
Re: [RFC PATCH 2/2] linux-user/mmap: Fix Clang 'type-limit-compare' warning
In reply to Philippe Mathieu-DaudéQuoted 3 lines
+#if
TARGET_ABI_BITS<TARGET_LONG_BITS/* Check if address fits target address space */ if ((unsigned long)host_addr + new_size > (abi_ulong)-1) {
It would be clearer if "#if TARGET_LONG_BITS > TARGET_ABI_BITS" is used, to match the comparison in if() statement.
Quoted 13 lines
/* Revert mremap() changes */linux-user/mmap.c +1
-1@@ -721,6 +722,7 @@ abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, errno =ENOMEM ; host_addr = MAP_FAILED; } +#endif /* TARGET_ABI_BITS < TARGET_LONG_BITS */ }if (host_addr ==MAP_FAILED ) { --2.21.3
Re: [RFC PATCH 2/2] linux-user/mmap: Fix Clang 'type-limit-compare' warning
In reply to Philippe Mathieu-DaudéQuoted 20 lines · Philippe Mathieu-Daudé
When building with Clang 10 on Fedora 32, we get:
CC
linux-user/mmap.o
linux-user/mmap.c:720:49: error: result of comparison 'unsigned long' > 18446744073709551615 is always false [-Werror,-Wtautological-type-limit-compare]if ((unsigned long)host_addr + new_size > (abi_ulong)-1) { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~Fix by restricting the check for when target
sizeof(abi_ulong)is smaller than target sizeof(unsigned long).Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- linux-user/mmap.c | 2 ++ 1 file changed, 2 insertions(+)linux-user/mmap.c +0
-0
linux-user/mmap.c abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, · line 719
Quoted 6 lines
linux-user/mmap.c +1
-0@@ -714,6 +714,7 @@ abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, errno =ENOMEM ; host_addr =MAP_FAILED ; } +#if TARGET_ABI_BITS <TARGET_LONG_BITS /* Check if address fits target address space */ if ((unsigned long )host_addr + new_size > (abi_ulong)-1 ) {
Instead of using #if, the following suffices to shut up clang:
linux-user/mmap.c +1 -1
@@ -715,7 +715,7 @@ abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, host_addr =MAP_FAILED ; }/* Check if address fits target address space */ -if ((unsigned long )host_addr + new_size > (abi_ulong)-1 ) { +if ((unsigned long )host_addr > (abi_ulong)-1 - new_size) {/* Revert mremap() changes */ host_addr =mremap (g2h (old_addr), new_size, old_size, flags); errno =ENOMEM ;
That is, it is no longer a tautological type compare if you commute the operations so that neither side is a compile-time constant.
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3226 Virtualization: qemu.org | libvirt.org
Re: [RFC PATCH 2/2] linux-user/mmap: Fix Clang 'type-limit-compare' warning
Quoted 17 lines · Eric Blake
Instead of using #if, the following suffices to shut up clang:
linux-user/mmap.c +1
-1@@ -715,7 +715,7 @@ abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, host_addr = MAP_FAILED; } /* Check if address fits target address space */ - if ((unsigned long)host_addr + new_size > (abi_ulong)-1) { + if ((unsigned long)host_addr > (abi_ulong)-1 - new_size) { /* Revert mremap() changes */ host_addr = mremap(g2h(old_addr), new_size, old_size, flags); errno = ENOMEM;
Quoted 2 lines
That is, it is no longer a tautological type compare if you commute the operations so that neither side is a compile-time constant.
To some extent the tautological compare is a hint to the compiler that the comparison may be optimized away. If sizeof(abi_ulong) >= sizeof(unsigned long), then the host *cannot* produce an out-of-range target address.
We could add the sizeof test to the if, to preserve the optimization, but that by itself doesn't prevent the clang warning.
Which is why I have repeatedly suggested that we disable this warning globally.
Because every single instance so far has been of this form: where we are
expecting the compiler to fold away the block of code protected by the tautological comparison.
I will also note that there is an existing off-by-one problem wrt the final
page: with a 12-bit page,
0xfffff000 + 0x1000 > 0xffffffff
The proper test I think is
(uintptr_t)host_addr + new_size - 1 > (abi_ulong)-1
If we're going to use your suggestion above, this becomes
(uintptr_t)host_addr > (abi_ulong)-new_size
r~
Re: [RFC PATCH 2/2] linux-user/mmap: Fix Clang 'type-limit-compare' warning
Quoted 25 lines · Richard Henderson
Quoted 17 lines · Eric Blake
Instead of using #if, the following suffices to shut up clang:
linux-user/mmap.c +1
-1@@ -715,7 +715,7 @@ abi_long target_mremap(abi_ulong old_addr, abi_ulong old_size, host_addr = MAP_FAILED; } /* Check if address fits target address space */ - if ((unsigned long)host_addr + new_size > (abi_ulong)-1) { + if ((unsigned long)host_addr > (abi_ulong)-1 - new_size) { /* Revert mremap() changes */ host_addr = mremap(g2h(old_addr), new_size, old_size, flags); errno = ENOMEM;Quoted 2 lines
That is, it is no longer a tautological type compare if you commute the operations so that neither side is a compile-time constant.
To some extent the tautological compare is a hint to the compiler that the comparison may be optimized away. If
sizeof(abi_ulong)>= sizeof(unsigned long), then the host *cannot* produce an out-of-range target address.
Quoted 4 lines
We could add the sizeof test to the if, to preserve the optimization, but that by itself doesn't prevent the clang warning.
Which is why I have repeatedly suggested that we disable this warning globally.
I guess most people (like me) don't have a strong opinion about this. So maybe simply suggest a patch to disable the warning? I don't think that anybody will object.
Thomas