[libvirt] [PATCH 08/34] Use g_strdup to fill in default values

Ján Tomko jtomko at redhat.com
Sun Oct 20 12:55:26 UTC 2019


Replace:
  if (!s && VIR_STRDUP(s, str) < 0)
    goto;
with:
  if (!s)
    s = g_strdup(str);

Signed-off-by: Ján Tomko <jtomko at redhat.com>
---
 src/conf/domain_conf.c                |  4 ++--
 src/conf/storage_conf.c               |  4 ++--
 src/esx/esx_util.c                    |  5 ++---
 src/esx/esx_vi_types.c                |  4 ++--
 src/hyperv/hyperv_util.c              |  5 ++---
 src/node_device/node_device_udev.c    |  4 ++--
 src/qemu/qemu_capabilities.c          |  5 ++---
 src/qemu/qemu_domain.c                |  5 ++---
 src/qemu/qemu_hotplug.c               | 14 ++++++--------
 src/remote/remote_driver.c            |  4 ++--
 src/security/security_apparmor.c      |  4 ++--
 src/security/security_selinux.c       |  9 ++++-----
 src/storage/storage_backend_logical.c |  4 ++--
 src/storage/storage_backend_zfs.c     |  4 ++--
 src/test/test_driver.c                |  4 ++--
 src/util/virfile.c                    |  8 ++++----
 src/util/virlease.c                   |  8 ++------
 src/util/virpci.c                     |  6 ++----
 src/util/virtypedparam.c              |  4 ++--
 src/vmx/vmx.c                         |  4 ++--
 tests/domaincapstest.c                |  6 ++----
 21 files changed, 50 insertions(+), 65 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index cd9b6ca993..d0dd7b3fa9 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -13140,8 +13140,8 @@ virDomainTPMDefParseXML(virDomainXMLOptionPtr xmlopt,
     switch (def->type) {
     case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH:
         path = virXPathString("string(./backend/device/@path)", ctxt);
-        if (!path && VIR_STRDUP(path, VIR_DOMAIN_TPM_DEFAULT_DEVICE) < 0)
-            goto error;
+        if (!path)
+            path = g_strdup(VIR_DOMAIN_TPM_DEFAULT_DEVICE);
         def->data.passthrough.source.data.file.path = g_steal_pointer(&path);
         def->data.passthrough.source.type = VIR_DOMAIN_CHR_TYPE_DEV;
         break;
diff --git a/src/conf/storage_conf.c b/src/conf/storage_conf.c
index 9e4978b560..ba3e3ec326 100644
--- a/src/conf/storage_conf.c
+++ b/src/conf/storage_conf.c
@@ -1414,8 +1414,8 @@ virStorageVolDefParseXML(virStoragePoolDefPtr pool,
         if ((n = virXPathNodeSet("./target/features/*", ctxt, &nodes)) < 0)
             return NULL;
 
-        if (!def->target.compat && VIR_STRDUP(def->target.compat, "1.1") < 0)
-            return NULL;
+        if (!def->target.compat)
+            def->target.compat = g_strdup("1.1");
 
         if (!(def->target.features = virBitmapNew(VIR_STORAGE_FILE_FEATURE_LAST)))
             return NULL;
diff --git a/src/esx/esx_util.c b/src/esx/esx_util.c
index d7210375fa..1bed01054b 100644
--- a/src/esx/esx_util.c
+++ b/src/esx/esx_util.c
@@ -156,9 +156,8 @@ esxUtil_ParseUri(esxUtil_ParsedUri **parsedUri, virURIPtr uri)
     if (VIR_STRDUP((*parsedUri)->path, uri->path) < 0)
         goto cleanup;
 
-    if (!(*parsedUri)->transport &&
-        VIR_STRDUP((*parsedUri)->transport, "https") < 0)
-        goto cleanup;
+    if (!(*parsedUri)->transport)
+        (*parsedUri)->transport = g_strdup("https");
 
     result = 0;
 
diff --git a/src/esx/esx_vi_types.c b/src/esx/esx_vi_types.c
index 29b1b01b9b..a32f674740 100644
--- a/src/esx/esx_vi_types.c
+++ b/src/esx/esx_vi_types.c
@@ -1034,8 +1034,8 @@ esxVI_AnyType_Deserialize(xmlNodePtr node, esxVI_AnyType **anyType)
     (*anyType)->value =
       (char *)xmlNodeListGetString(node->doc, node->children, 1);
 
-    if (!(*anyType)->value && VIR_STRDUP((*anyType)->value, "") < 0)
-        goto failure;
+    if (!(*anyType)->value)
+        (*anyType)->value = g_strdup("");
 
 #define _DESERIALIZE_NUMBER(_type, _xsdType, _name, _min, _max) \
         do { \
diff --git a/src/hyperv/hyperv_util.c b/src/hyperv/hyperv_util.c
index 73b32694a1..3b84713426 100644
--- a/src/hyperv/hyperv_util.c
+++ b/src/hyperv/hyperv_util.c
@@ -71,9 +71,8 @@ hypervParseUri(hypervParsedUri **parsedUri, virURIPtr uri)
         }
     }
 
-    if (!(*parsedUri)->transport &&
-        VIR_STRDUP((*parsedUri)->transport, "https") < 0)
-        goto cleanup;
+    if (!(*parsedUri)->transport)
+        (*parsedUri)->transport = g_strdup("https");
 
     result = 0;
 
diff --git a/src/node_device/node_device_udev.c b/src/node_device/node_device_udev.c
index c9449ace9e..84f69da8de 100644
--- a/src/node_device/node_device_udev.c
+++ b/src/node_device/node_device_udev.c
@@ -1307,8 +1307,8 @@ udevSetParent(struct udev_device *device,
 
     } while (def->parent == NULL && parent_device != NULL);
 
-    if (!def->parent && VIR_STRDUP(def->parent, "computer") < 0)
-        goto cleanup;
+    if (!def->parent)
+        def->parent = g_strdup("computer");
 
     ret = 0;
 
diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index 3ce1556fc2..0377cea51a 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -3735,9 +3735,8 @@ virQEMUCapsLoadCache(virArch hostArch,
 
     if (virXPathBoolean("boolean(./package)", ctxt) > 0) {
         qemuCaps->package = virXPathString("string(./package)", ctxt);
-        if (!qemuCaps->package &&
-            VIR_STRDUP(qemuCaps->package, "") < 0)
-            goto cleanup;
+        if (!qemuCaps->package)
+            qemuCaps->package = g_strdup("");
     }
 
     if (virXPathBoolean("boolean(./kernelVersion)", ctxt) > 0) {
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index 3ef84cc9a6..ab13eebd39 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -13339,9 +13339,8 @@ qemuDomainSetupDev(virQEMUDriverConfigPtr cfg,
 
     mount_options = qemuSecurityGetMountOptions(mgr, vm->def);
 
-    if (!mount_options &&
-        VIR_STRDUP(mount_options, "") < 0)
-        goto cleanup;
+    if (!mount_options)
+        mount_options = g_strdup("");
 
     /*
      * tmpfs is limited to 64kb, since we only have device nodes in there
diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
index bf301919cc..5b42bbfd48 100644
--- a/src/qemu/qemu_hotplug.c
+++ b/src/qemu/qemu_hotplug.c
@@ -3574,8 +3574,8 @@ qemuDomainChangeNet(virQEMUDriverPtr driver,
     }
 
     /* ifname: check if it's set in newdev. If not, retain the autogenerated one */
-    if (!newdev->ifname && VIR_STRDUP(newdev->ifname, olddev->ifname) < 0)
-        goto cleanup;
+    if (!newdev->ifname)
+        newdev->ifname = g_strdup(olddev->ifname);
     if (STRNEQ_NULLABLE(olddev->ifname, newdev->ifname)) {
         virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
                        _("cannot modify network device tap name"));
@@ -3603,9 +3603,8 @@ qemuDomainChangeNet(virQEMUDriverPtr driver,
         goto cleanup;
     }
     /* grab alias from olddev if not set in newdev */
-    if (!newdev->info.alias &&
-        VIR_STRDUP(newdev->info.alias, olddev->info.alias) < 0)
-        goto cleanup;
+    if (!newdev->info.alias)
+        newdev->info.alias = g_strdup(olddev->info.alias);
 
     /* device alias is checked already in virDomainDefCompatibleDevice */
 
@@ -3617,9 +3616,8 @@ qemuDomainChangeNet(virQEMUDriverPtr driver,
         goto cleanup;
     }
 
-    if (!newdev->info.romfile &&
-        VIR_STRDUP(newdev->info.romfile, olddev->info.romfile) < 0)
-        goto cleanup;
+    if (!newdev->info.romfile)
+        newdev->info.romfile = g_strdup(olddev->info.romfile);
     if (STRNEQ_NULLABLE(olddev->info.romfile, newdev->info.romfile)) {
         virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
                        _("cannot modify network rom file"));
diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c
index 9228c7d0ed..c22b257197 100644
--- a/src/remote/remote_driver.c
+++ b/src/remote/remote_driver.c
@@ -1247,8 +1247,8 @@ doRemoteOpen(virConnectPtr conn,
         break;
 
     case REMOTE_DRIVER_TRANSPORT_SSH:
-        if (!command && VIR_STRDUP(command, "ssh") < 0)
-            goto failed;
+        if (!command)
+            command = g_strdup("ssh");
 
         if (!(priv->client = virNetClientNewSSH(priv->hostname,
                                                 port,
diff --git a/src/security/security_apparmor.c b/src/security/security_apparmor.c
index dd8f7d3808..82826d193c 100644
--- a/src/security/security_apparmor.c
+++ b/src/security/security_apparmor.c
@@ -459,8 +459,8 @@ AppArmorGenSecurityLabel(virSecurityManagerPtr mgr G_GNUC_UNUSED,
     if (VIR_STRDUP(secdef->imagelabel, profile_name) < 0)
         goto err;
 
-    if (!secdef->model && VIR_STRDUP(secdef->model, SECURITY_APPARMOR_NAME) < 0)
-        goto err;
+    if (!secdef->model)
+        secdef->model = g_strdup(SECURITY_APPARMOR_NAME);
 
     /* Now that we have a label, load the profile into the kernel. */
     if (load_profile(mgr, secdef->label, def, NULL, false) < 0) {
diff --git a/src/security/security_selinux.c b/src/security/security_selinux.c
index 4988659044..c24a63afd6 100644
--- a/src/security/security_selinux.c
+++ b/src/security/security_selinux.c
@@ -938,9 +938,8 @@ virSecuritySELinuxGenLabel(virSecurityManagerPtr mgr,
     if (!seclabel->imagelabel)
         goto cleanup;
 
-    if (!seclabel->model &&
-        VIR_STRDUP(seclabel->model, SECURITY_SELINUX_NAME) < 0)
-        goto cleanup;
+    if (!seclabel->model)
+        seclabel->model = g_strdup(SECURITY_SELINUX_NAME);
 
     rc = 0;
 
@@ -3368,8 +3367,8 @@ virSecuritySELinuxGetSecurityMountOptions(virSecurityManagerPtr mgr,
             return NULL;
     }
 
-    if (!opts && VIR_STRDUP(opts, "") < 0)
-        return NULL;
+    if (!opts)
+        opts = g_strdup("");
 
     VIR_DEBUG("imageLabel=%s opts=%s",
               secdef ? secdef->imagelabel : "(null)", opts);
diff --git a/src/storage/storage_backend_logical.c b/src/storage/storage_backend_logical.c
index 5de21103f5..80d2dfd0b4 100644
--- a/src/storage/storage_backend_logical.c
+++ b/src/storage/storage_backend_logical.c
@@ -325,8 +325,8 @@ virStorageBackendLogicalMakeVol(char **const groups,
         vol->target.backingStore->type = VIR_STORAGE_TYPE_BLOCK;
     }
 
-    if (!vol->key && VIR_STRDUP(vol->key, groups[2]) < 0)
-        goto cleanup;
+    if (!vol->key)
+        vol->key = g_strdup(groups[2]);
 
     if (virStorageBackendUpdateVolInfo(vol, false,
                                        VIR_STORAGE_VOL_OPEN_DEFAULT, 0) < 0)
diff --git a/src/storage/storage_backend_zfs.c b/src/storage/storage_backend_zfs.c
index 6bfff9f5dd..75edab4b26 100644
--- a/src/storage/storage_backend_zfs.c
+++ b/src/storage/storage_backend_zfs.c
@@ -136,8 +136,8 @@ virStorageBackendZFSParseVol(virStoragePoolObjPtr pool,
             goto cleanup;
     }
 
-    if (!volume->key && VIR_STRDUP(volume->key, tokens[0]) < 0)
-        goto cleanup;
+    if (!volume->key)
+        volume->key = g_strdup(tokens[0]);
 
     if (volume->target.path == NULL) {
         if (virAsprintf(&volume->target.path, "%s/%s",
diff --git a/src/test/test_driver.c b/src/test/test_driver.c
index bd095604c7..6af40ee35c 100644
--- a/src/test/test_driver.c
+++ b/src/test/test_driver.c
@@ -1100,8 +1100,8 @@ testOpenVolumesForPool(const char *file,
                 return -1;
         }
 
-        if (!volDef->key && VIR_STRDUP(volDef->key, volDef->target.path) < 0)
-            return -1;
+        if (!volDef->key)
+            volDef->key = g_strdup(volDef->target.path);
 
         if (virStoragePoolObjAddVol(obj, volDef) < 0)
             return -1;
diff --git a/src/util/virfile.c b/src/util/virfile.c
index 15713a58d9..6a26d4838c 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -4008,12 +4008,12 @@ virFileComparePaths(const char *p1, const char *p2)
      * comparison.
      */
     ignore_value(virFileResolveLink(p1, &res1));
-    if (!res1 && VIR_STRDUP(res1, p1) < 0)
-        return -1;
+    if (!res1)
+        res1 = g_strdup(p1);
 
     ignore_value(virFileResolveLink(p2, &res2));
-    if (!res2 && VIR_STRDUP(res2, p2) < 0)
-        return -1;
+    if (!res2)
+        res2 = g_strdup(p2);
 
     return STREQ_NULLABLE(res1, res2);
 }
diff --git a/src/util/virlease.c b/src/util/virlease.c
index a49bd5b9fb..3d851eb808 100644
--- a/src/util/virlease.c
+++ b/src/util/virlease.c
@@ -106,12 +106,8 @@ virLeaseReadCustomLeaseFile(virJSONValuePtr leases_array_new,
             /* This is an ipv6 lease */
             if ((server_duid_tmp
                  = virJSONValueObjectGetString(lease_tmp, "server-duid"))) {
-                if (!*server_duid && VIR_STRDUP(*server_duid, server_duid_tmp) < 0) {
-                    /* Control reaches here when the 'action' is not for an
-                     * ipv6 lease or, for some weird reason the env var
-                     * DNSMASQ_SERVER_DUID wasn't set*/
-                    return -1;
-                }
+                if (!*server_duid)
+                    *server_duid = g_strdup(server_duid_tmp);
             } else {
                 /* Inject server-duid into those ipv6 leases which
                  * didn't have it previously, for example, those
diff --git a/src/util/virpci.c b/src/util/virpci.c
index 2c54c8baa1..59a93a5e81 100644
--- a/src/util/virpci.c
+++ b/src/util/virpci.c
@@ -2493,10 +2493,8 @@ virPCIGetNetName(const char *device_link_sysfs_path,
                  * needed because some NIC drivers (e.g. i40e)
                  * implement phys_port_id for PFs, but not for VFs
                  */
-                if (!firstEntryName &&
-                    VIR_STRDUP(firstEntryName, entry->d_name) < 0) {
-                    goto cleanup;
-                }
+                if (!firstEntryName)
+                    firstEntryName = g_strdup(entry->d_name);
 
                 continue;
             }
diff --git a/src/util/virtypedparam.c b/src/util/virtypedparam.c
index c2dadfd410..db440611dc 100644
--- a/src/util/virtypedparam.c
+++ b/src/util/virtypedparam.c
@@ -240,8 +240,8 @@ virTypedParameterAssignValueVArgs(virTypedParameterPtr param,
             param->value.s = va_arg(ap, char *);
         }
 
-        if (!param->value.s && VIR_STRDUP(param->value.s, "") < 0)
-            return -1;
+        if (!param->value.s)
+            param->value.s = g_strdup("");
         break;
     default:
         virReportError(VIR_ERR_INTERNAL_ERROR,
diff --git a/src/vmx/vmx.c b/src/vmx/vmx.c
index 7d20351a83..c9a80ba35d 100644
--- a/src/vmx/vmx.c
+++ b/src/vmx/vmx.c
@@ -2659,8 +2659,8 @@ virVMXParseEthernet(virConfPtr conf, int controller, virDomainNetDefPtr *def)
                                   true) < 0)
             goto cleanup;
 
-        if (!networkName && VIR_STRDUP(networkName, "") < 0)
-            goto cleanup;
+        if (!networkName)
+            networkName = g_strdup("");
     }
 
     /* vmx:vnet -> def:data.ifname */
diff --git a/tests/domaincapstest.c b/tests/domaincapstest.c
index db14114cba..07bc99cc03 100644
--- a/tests/domaincapstest.c
+++ b/tests/domaincapstest.c
@@ -100,10 +100,8 @@ fillQemuCaps(virDomainCapsPtr domCaps,
             goto cleanup;
     }
 
-    if (!domCaps->machine &&
-        VIR_STRDUP(domCaps->machine,
-                   virQEMUCapsGetPreferredMachine(qemuCaps)) < 0)
-        goto cleanup;
+    if (!domCaps->machine)
+        domCaps->machine = g_strdup(virQEMUCapsGetPreferredMachine(qemuCaps));
 
     if (virQEMUCapsFillDomainCaps(caps, domCaps, qemuCaps,
                                   false,
-- 
2.21.0




More information about the libvir-list mailing list