[PATCH] util: Make virStringArrayHasString() const-correct

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The first argument should be const char ** instead of
char **, because this is a search function and as such it
doesn't, and shouldn't, alter the haystack in any way.

This change means we no longer have to cast arrays of
immutable strings to arrays of mutable strings; we still
have to do the opposite, though, but that's reasonable.
---
 src/lxc/lxc_native.c         |  5 +++--
 src/qemu/qemu_capabilities.c |  4 ++--
 src/qemu/qemu_monitor_json.c |  2 +-
 src/qemu/qemu_process.c      |  4 +++-
 src/util/virstring.c         |  3 ++-
 src/util/virstring.h         |  2 +-
 tests/qemumonitorjsontest.c  | 10 +++++-----
 7 files changed, 17 insertions(+), 13 deletions(-)

diff --git a/src/lxc/lxc_native.c b/src/lxc/lxc_native.c
index 4b22e2a..94fb659 100644
--- a/src/lxc/lxc_native.c
+++ b/src/lxc/lxc_native.c
@@ -299,7 +299,7 @@ lxcAddFstabLine(virDomainDefPtr def, lxcFstabPtr fstab)
         type = VIR_DOMAIN_FS_TYPE_BLOCK;
 
     /* Do we have ro in options? */
-    readonly = virStringArrayHasString(options, "ro");
+    readonly = virStringArrayHasString((const char **) options, "ro");
 
     if (lxcAddFSDef(def, type, src, dst, readonly, usage) < 0)
         goto cleanup;
@@ -981,7 +981,8 @@ lxcSetCapDrop(virDomainDefPtr def, virConfPtr properties)
 
     for (i = 0; i < VIR_DOMAIN_CAPS_FEATURE_LAST; i++) {
         capString = virDomainCapsFeatureTypeToString(i);
-        if (toDrop != NULL && virStringArrayHasString(toDrop, capString))
+        if (toDrop != NULL &&
+            virStringArrayHasString((const char **) toDrop, capString))
             def->caps_features[i] = VIR_TRISTATE_SWITCH_OFF;
     }
 
diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index 43e3ea7..1483217 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -2580,7 +2580,7 @@ virQEMUCapsProbeQMPTPM(virQEMUCapsPtr qemuCaps,
         for (i = 0; i < ARRAY_CARDINALITY(virQEMUCapsTPMModelsToCaps); i++) {
             const char *needle = virDomainTPMModelTypeToString(
                 virQEMUCapsTPMModelsToCaps[i].type);
-            if (virStringArrayHasString(entries, needle))
+            if (virStringArrayHasString((const char **) entries, needle))
                 virQEMUCapsSet(qemuCaps,
                                virQEMUCapsTPMModelsToCaps[i].caps);
         }
@@ -2594,7 +2594,7 @@ virQEMUCapsProbeQMPTPM(virQEMUCapsPtr qemuCaps,
         for (i = 0; i < ARRAY_CARDINALITY(virQEMUCapsTPMTypesToCaps); i++) {
             const char *needle = virDomainTPMBackendTypeToString(
                 virQEMUCapsTPMTypesToCaps[i].type);
-            if (virStringArrayHasString(entries, needle))
+            if (virStringArrayHasString((const char **) entries, needle))
                 virQEMUCapsSet(qemuCaps, virQEMUCapsTPMTypesToCaps[i].caps);
         }
     }
diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index d455adf..1df1e4a 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -5728,7 +5728,7 @@ qemuMonitorJSONGetMigrationCapability(qemuMonitorPtr mon,
     if (qemuMonitorJSONGetMigrationCapabilities(mon, &capsList) < 0)
         return -1;
 
-    ret = virStringArrayHasString(capsList, cap);
+    ret = virStringArrayHasString((const char **) capsList, cap);
 
     virStringFreeList(capsList);
     return ret;
diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index 7481626..3ade190 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -3151,6 +3151,7 @@ qemuProcessUpdateDevices(virQEMUDriverPtr driver,
 {
     qemuDomainObjPrivatePtr priv = vm->privateData;
     virDomainDeviceDef dev;
+    const char **qemuDevices;
     char **old;
     char **tmp;
     int ret = -1;
@@ -3163,9 +3164,10 @@ qemuProcessUpdateDevices(virQEMUDriverPtr driver,
     if (qemuDomainUpdateDeviceList(driver, vm, QEMU_ASYNC_JOB_NONE) < 0)
         goto cleanup;
 
+    qemuDevices = (const char **) priv->qemuDevices;
     if ((tmp = old)) {
         while (*tmp) {
-            if (!virStringArrayHasString(priv->qemuDevices, *tmp) &&
+            if (!virStringArrayHasString(qemuDevices, *tmp) &&
                 virDomainDefFindDevice(vm->def, *tmp, &dev, false) == 0 &&
                 qemuDomainRemoveDevice(driver, vm, &dev) < 0) {
                 goto cleanup;
diff --git a/src/util/virstring.c b/src/util/virstring.c
index 0177a95..4a70620 100644
--- a/src/util/virstring.c
+++ b/src/util/virstring.c
@@ -210,7 +210,8 @@ virStringFreeListCount(char **strings,
 
 
 bool
-virStringArrayHasString(char **strings, const char *needle)
+virStringArrayHasString(const char **strings,
+                        const char *needle)
 {
     size_t i = 0;
 
diff --git a/src/util/virstring.h b/src/util/virstring.h
index 040771e..8854d5f 100644
--- a/src/util/virstring.h
+++ b/src/util/virstring.h
@@ -44,7 +44,7 @@ char *virStringJoin(const char **strings,
 void virStringFreeList(char **strings);
 void virStringFreeListCount(char **strings, size_t count);
 
-bool virStringArrayHasString(char **strings, const char *needle);
+bool virStringArrayHasString(const char **strings, const char *needle);
 char *virStringGetFirstWithPrefix(char **strings, const char *prefix)
     ATTRIBUTE_NONNULL(2);
 
diff --git a/tests/qemumonitorjsontest.c b/tests/qemumonitorjsontest.c
index 544b569..9988754 100644
--- a/tests/qemumonitorjsontest.c
+++ b/tests/qemumonitorjsontest.c
@@ -999,7 +999,7 @@ testQemuMonitorJSONGetDeviceAliases(const void *data)
     qemuMonitorTestPtr test = qemuMonitorTestNewSimple(true, xmlopt);
     int ret = -1;
     char **aliases = NULL;
-    char **alias;
+    const char **alias;
     const char *expected[] = {
         "virtio-disk25", "video0", "serial0", "ide0-0-0", "usb", NULL };
 
@@ -1033,14 +1033,14 @@ testQemuMonitorJSONGetDeviceAliases(const void *data)
     }
 
     ret = 0;
-    for (alias = aliases; *alias; alias++) {
-        if (!virStringArrayHasString((char **) expected, *alias)) {
+    for (alias = (const char **) aliases; *alias; alias++) {
+        if (!virStringArrayHasString(expected, *alias)) {
             fprintf(stderr, "got unexpected device alias '%s'\n", *alias);
             ret = -1;
         }
     }
-    for (alias = (char **) expected; *alias; alias++) {
-        if (!virStringArrayHasString(aliases, *alias)) {
+    for (alias = expected; *alias; alias++) {
+        if (!virStringArrayHasString((const char **) aliases, *alias)) {
             fprintf(stderr, "missing expected alias '%s'\n", *alias);
             ret = -1;
         }
-- 
2.7.4

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



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]