[libvirt] [PATCH] remote_daemon_dispatch.c: typecast ARRAY_CARDINALITY() in remoteDispatchProbeURI()

Michal Privoznik posted 1 patch 4 years, 8 months ago
Test syntax-check passed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/20190814131318.117948-1-mprivozn@redhat.com
src/remote/remote_daemon_dispatch.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
[libvirt] [PATCH] remote_daemon_dispatch.c: typecast ARRAY_CARDINALITY() in remoteDispatchProbeURI()
Posted by Michal Privoznik 4 years, 8 months ago
Since users can enable/disable drivers at compile time, it may
happen that @drivers array is in fact empty (in both its
occurrences within the function). This means that
ARRAY_CARDINALITY() returns 0UL which makes gcc unhappy because
of loop condition:

  i < ARRAY_CARDINALITY(drivers)

GCC complains that @i is unsigned and comparing an unsigned value
against 0 is always false. However, changing the type of @i to
ssize_t is not enough, because compiler still sees the unsigned
zero. The solution is to typecast the ARRAY_CARDINALITY().

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---
 src/remote/remote_daemon_dispatch.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/remote/remote_daemon_dispatch.c b/src/remote/remote_daemon_dispatch.c
index c8e353ebd3..1bd281dd6d 100644
--- a/src/remote/remote_daemon_dispatch.c
+++ b/src/remote/remote_daemon_dispatch.c
@@ -2141,9 +2141,9 @@ remoteDispatchProbeURI(bool readonly,
             "vbox",
 # endif
         };
-        size_t i;
+        ssize_t i;
 
-        for (i = 0; i < ARRAY_CARDINALITY(drivers) && !*probeduri; i++) {
+        for (i = 0; i < (ssize_t) ARRAY_CARDINALITY(drivers) && !*probeduri; i++) {
             VIR_AUTOFREE(char *) daemonname = NULL;
             VIR_AUTOFREE(char *) daemonpath = NULL;
 
@@ -2187,9 +2187,9 @@ remoteDispatchProbeURI(bool readonly,
             "vz",
 # endif
         };
-        size_t i;
+        ssize_t i;
 
-        for (i = 0; i < ARRAY_CARDINALITY(drivers) && !*probeduri; i++) {
+        for (i = 0; i < (ssize_t) ARRAY_CARDINALITY(drivers) && !*probeduri; i++) {
             VIR_AUTOFREE(char *) sockname = NULL;
 
             if (virAsprintf(&sockname, "%s/run/libvirt/virt%sd-%s",
-- 
2.21.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH] remote_daemon_dispatch.c: typecast ARRAY_CARDINALITY() in remoteDispatchProbeURI()
Posted by Jim Fehlig 4 years, 8 months ago
On 8/14/19 7:13 AM, Michal Privoznik wrote:
> Since users can enable/disable drivers at compile time, it may
> happen that @drivers array is in fact empty (in both its
> occurrences within the function). This means that
> ARRAY_CARDINALITY() returns 0UL which makes gcc unhappy because
> of loop condition:
> 
>    i < ARRAY_CARDINALITY(drivers)
> 
> GCC complains that @i is unsigned and comparing an unsigned value
> against 0 is always false. However, changing the type of @i to
> ssize_t is not enough, because compiler still sees the unsigned
> zero. The solution is to typecast the ARRAY_CARDINALITY().
> 
> Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
> ---
>   src/remote/remote_daemon_dispatch.c | 8 ++++----
>   1 file changed, 4 insertions(+), 4 deletions(-)

Typing the original mail and reviewing and testing this patch probably took 
longer than fixing it in the first place :-). Thanks for taking care of it!

Reviewed-by: Jim Fehlig <jfehlig@suse.com>

Regards,
Jim

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list