[libvirt] [PATCH python] Avoid implicit treatment of an arithmetic result as a boolean

Daniel P. Berrange posted 1 patch 6 years, 7 months ago
Failed in applying to current master (apply log)
libvirt-utils.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[libvirt] [PATCH python] Avoid implicit treatment of an arithmetic result as a boolean
Posted by Daniel P. Berrange 6 years, 7 months ago
Latest GCC versions are unhappy with us treating an integer
arithmetic result as a boolean:

libvirt-utils.c: In function ‘virReallocN’:
libvirt-utils.c:111:23: warning: ‘*’ in boolean context, suggest ‘&&’ instead [-Wint-in-bool-context]
     if (!tmp && (size * count)) {
                 ~~~~~~^~~~~~~~

Add an explicit comparison '!= 0' to keep it happy, since its
suggestion to use '&&' is nonsense.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
---
 libvirt-utils.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libvirt-utils.c b/libvirt-utils.c
index b8ac5e9..f7b4478 100644
--- a/libvirt-utils.c
+++ b/libvirt-utils.c
@@ -108,7 +108,7 @@ virReallocN(void *ptrptr,
         return -1;
     }
     tmp = realloc(*(void**)ptrptr, size * count);
-    if (!tmp && (size * count)) {
+    if (!tmp && ((size * count) != 0)) {
         return -1;
     }
     *(void**)ptrptr = tmp;
-- 
2.13.5

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH python] Avoid implicit treatment of an arithmetic result as a boolean
Posted by Pavel Hrdina 6 years, 7 months ago
On Tue, Sep 26, 2017 at 11:15:36AM +0100, Daniel P. Berrange wrote:
> Latest GCC versions are unhappy with us treating an integer
> arithmetic result as a boolean:
> 
> libvirt-utils.c: In function ‘virReallocN’:
> libvirt-utils.c:111:23: warning: ‘*’ in boolean context, suggest ‘&&’ instead [-Wint-in-bool-context]
>      if (!tmp && (size * count)) {
>                  ~~~~~~^~~~~~~~
> 
> Add an explicit comparison '!= 0' to keep it happy, since its
> suggestion to use '&&' is nonsense.
> 
> Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
> ---
>  libvirt-utils.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/libvirt-utils.c b/libvirt-utils.c
> index b8ac5e9..f7b4478 100644
> --- a/libvirt-utils.c
> +++ b/libvirt-utils.c
> @@ -108,7 +108,7 @@ virReallocN(void *ptrptr,
>          return -1;
>      }
>      tmp = realloc(*(void**)ptrptr, size * count);
> -    if (!tmp && (size * count)) {
> +    if (!tmp && ((size * count) != 0)) {

There is no need for the extra set of brackets:

    if (!tmp && (size * count) != 0) {...}

but that's a matter of personal preference.

Reviewed-by: Pavel Hrdina <phrdina@redhat.com>
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list