[libvirt] one more warning

Jim Meyering jim at meyering.net
Mon Oct 27 18:49:17 UTC 2008


One more warning question:
Compiling with all storage backends disabled, I get this:

storage_backend.c:91: warning: comparison of unsigned expression < 0 is always false

That's because of this code:

static virStorageBackendPtr backends[] = {
#if WITH_STORAGE_DIR
    &virStorageBackendDirectory,
#endif
#if WITH_STORAGE_FS
    &virStorageBackendFileSystem,
    &virStorageBackendNetFileSystem,
#endif
#if WITH_STORAGE_LVM
    &virStorageBackendLogical,
#endif
#if WITH_STORAGE_ISCSI
    &virStorageBackendISCSI,
#endif
#if WITH_STORAGE_DISK
    &virStorageBackendDisk,
#endif
};


virStorageBackendPtr
virStorageBackendForType(int type) {
    unsigned int i;
    for (i = 0 ; i < ARRAY_CARDINALITY(backends); i++)
        if (backends[i]->type == type)
            return backends[i];

    virStorageReportError(NULL, VIR_ERR_INTERNAL_ERROR,
                          _("missing backend for pool type %d"), type);
    return NULL;
}

The above loses because ARRAY_CARDINALITY(backends) is 0.

One solution is to always have at least one backend,
e.g., the first one, in which case, this patch works fine:
[but if you like this (i don't), it'd make sense also to remove the
configure-time option ]

diff --git a/src/storage_backend.c b/src/storage_backend.c
index e33f98c..264cc53 100644
--- a/src/storage_backend.c
+++ b/src/storage_backend.c
@@ -56,9 +56,7 @@
 #if WITH_STORAGE_DISK
 #include "storage_backend_disk.h"
 #endif
-#if WITH_STORAGE_DIR
 #include "storage_backend_fs.h"
-#endif

 VIR_ENUM_IMPL(virStorageBackendPartTable,
               VIR_STORAGE_POOL_DISK_LAST,
@@ -66,9 +64,7 @@ VIR_ENUM_IMPL(virStorageBackendPartTable,
               "mac", "bsd", "pc98", "sun", "lvm2");

 static virStorageBackendPtr backends[] = {
-#if WITH_STORAGE_DIR
     &virStorageBackendDirectory,
-#endif
 #if WITH_STORAGE_FS
     &virStorageBackendFileSystem,
     &virStorageBackendNetFileSystem,

Another way is to add a NULL pointer at the end
and change the loop not to use the array size at all:

    for (i = 0; backends[i]; i++)
        if (backends[i]->type == type)
            return backends[i];

I chose the latter:

diff --git a/src/storage_backend.c b/src/storage_backend.c
index e33f98c..1f4ed10 100644
--- a/src/storage_backend.c
+++ b/src/storage_backend.c
@@ -82,13 +82,14 @@ static virStorageBackendPtr backends[] = {
 #if WITH_STORAGE_DISK
     &virStorageBackendDisk,
 #endif
+    NULL
 };


 virStorageBackendPtr
 virStorageBackendForType(int type) {
     unsigned int i;
-    for (i = 0 ; i < ARRAY_CARDINALITY(backends); i++)
+    for (i = 0; backends[i]; i++)
         if (backends[i]->type == type)
             return backends[i];




More information about the libvir-list mailing list