[libvirt] [PATCH 33/34] tools: use g_strdup instead of VIR_STRDUP

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


Replace all occurrences of
  if (VIR_STRDUP(a, b) < 0)
     /* effectively dead code */
with:
  a = g_strdup(b);

Signed-off-by: Ján Tomko <jtomko at redhat.com>
---
 tools/virsh-completer-checkpoint.c |  3 +--
 tools/virsh-completer-domain.c     | 15 +++++----------
 tools/virsh-completer-interface.c  |  3 +--
 tools/virsh-completer-network.c    | 14 ++++++--------
 tools/virsh-completer-nodedev.c    | 15 +++++----------
 tools/virsh-completer-nwfilter.c   |  6 ++----
 tools/virsh-completer-pool.c       | 15 +++++----------
 tools/virsh-completer-secret.c     | 10 ++++------
 tools/virsh-completer-snapshot.c   |  3 +--
 tools/virsh-completer-volume.c     |  3 +--
 tools/virsh-completer.c            |  8 ++++----
 tools/virt-admin-completer.c       |  3 +--
 tools/virt-login-shell-helper.c    | 17 +++++------------
 tools/vsh-table.c                  |  3 +--
 tools/vsh.c                        | 11 ++++-------
 15 files changed, 46 insertions(+), 83 deletions(-)

diff --git a/tools/virsh-completer-checkpoint.c b/tools/virsh-completer-checkpoint.c
index ce9d32844d..21e8bb5d3b 100644
--- a/tools/virsh-completer-checkpoint.c
+++ b/tools/virsh-completer-checkpoint.c
@@ -56,8 +56,7 @@ virshCheckpointNameCompleter(vshControl *ctl,
     for (i = 0; i < ncheckpoints; i++) {
         const char *name = virDomainCheckpointGetName(checkpoints[i]);
 
-        if (VIR_STRDUP(ret[i], name) < 0)
-            goto error;
+        ret[i] = g_strdup(name);
 
         virshDomainCheckpointFree(checkpoints[i]);
     }
diff --git a/tools/virsh-completer-domain.c b/tools/virsh-completer-domain.c
index bb06f468d7..6bfa3ab58c 100644
--- a/tools/virsh-completer-domain.c
+++ b/tools/virsh-completer-domain.c
@@ -62,8 +62,7 @@ virshDomainNameCompleter(vshControl *ctl,
     for (i = 0; i < ndomains; i++) {
         const char *name = virDomainGetName(domains[i]);
 
-        if (VIR_STRDUP(tmp[i], name) < 0)
-            goto cleanup;
+        tmp[i] = g_strdup(name);
     }
 
     ret = g_steal_pointer(&tmp);
@@ -181,10 +180,8 @@ virshDomainEventNameCompleter(vshControl *ctl G_GNUC_UNUSED,
     if (VIR_ALLOC_N(tmp, VIR_DOMAIN_EVENT_ID_LAST + 1) < 0)
         return NULL;
 
-    for (i = 0; i < VIR_DOMAIN_EVENT_ID_LAST; i++) {
-        if (VIR_STRDUP(tmp[i], virshDomainEventCallbacks[i].name) < 0)
-            return NULL;
-    }
+    for (i = 0; i < VIR_DOMAIN_EVENT_ID_LAST; i++)
+        tmp[i] = g_strdup(virshDomainEventCallbacks[i].name);
 
     ret = g_steal_pointer(&tmp);
     return ret;
@@ -242,11 +239,9 @@ virshDomainInterfaceStateCompleter(vshControl *ctl,
 
     if ((state = virXPathString("string(./link/@state)", ctxt)) &&
         STREQ(state, "down")) {
-        if (VIR_STRDUP(tmp[0], "up") < 0)
-            return NULL;
+        tmp[0] = g_strdup("up");
     } else {
-        if (VIR_STRDUP(tmp[0], "down") < 0)
-            return NULL;
+        tmp[0] = g_strdup("down");
     }
 
     ret = g_steal_pointer(&tmp);
diff --git a/tools/virsh-completer-interface.c b/tools/virsh-completer-interface.c
index 6fae0f6584..417374322a 100644
--- a/tools/virsh-completer-interface.c
+++ b/tools/virsh-completer-interface.c
@@ -53,8 +53,7 @@ virshInterfaceNameCompleter(vshControl *ctl,
     for (i = 0; i < nifaces; i++) {
         const char *name = virInterfaceGetName(ifaces[i]);
 
-        if (VIR_STRDUP(tmp[i], name) < 0)
-            goto cleanup;
+        tmp[i] = g_strdup(name);
     }
 
     ret = g_steal_pointer(&tmp);
diff --git a/tools/virsh-completer-network.c b/tools/virsh-completer-network.c
index a3d8b71d19..0097fca7cc 100644
--- a/tools/virsh-completer-network.c
+++ b/tools/virsh-completer-network.c
@@ -55,8 +55,7 @@ virshNetworkNameCompleter(vshControl *ctl,
     for (i = 0; i < nnets; i++) {
         const char *name = virNetworkGetName(nets[i]);
 
-        if (VIR_STRDUP(tmp[i], name) < 0)
-            goto cleanup;
+        tmp[i] = g_strdup(name);
     }
 
     ret = g_steal_pointer(&tmp);
@@ -83,10 +82,8 @@ virshNetworkEventNameCompleter(vshControl *ctl G_GNUC_UNUSED,
     if (VIR_ALLOC_N(tmp, VIR_NETWORK_EVENT_ID_LAST + 1) < 0)
         goto cleanup;
 
-    for (i = 0; i < VIR_NETWORK_EVENT_ID_LAST; i++) {
-        if (VIR_STRDUP(tmp[i], virshNetworkEventCallbacks[i].name) < 0)
-            goto cleanup;
-    }
+    for (i = 0; i < VIR_NETWORK_EVENT_ID_LAST; i++)
+        tmp[i] = g_strdup(virshNetworkEventCallbacks[i].name);
 
     ret = g_steal_pointer(&tmp);
 
@@ -124,10 +121,11 @@ virshNetworkPortUUIDCompleter(vshControl *ctl,
     for (i = 0; i < nports; i++) {
         char uuid[VIR_UUID_STRING_BUFLEN];
 
-        if (virNetworkPortGetUUIDString(ports[i], uuid) < 0 ||
-            VIR_STRDUP(ret[i], uuid) < 0)
+        if (virNetworkPortGetUUIDString(ports[i], uuid) < 0)
             goto error;
 
+        ret[i] = g_strdup(uuid);
+
         virNetworkPortFree(ports[i]);
     }
     VIR_FREE(ports);
diff --git a/tools/virsh-completer-nodedev.c b/tools/virsh-completer-nodedev.c
index 899c199902..6b01941911 100644
--- a/tools/virsh-completer-nodedev.c
+++ b/tools/virsh-completer-nodedev.c
@@ -53,8 +53,7 @@ virshNodeDeviceNameCompleter(vshControl *ctl,
     for (i = 0; i < ndevs; i++) {
         const char *name = virNodeDeviceGetName(devs[i]);
 
-        if (VIR_STRDUP(tmp[i], name) < 0)
-            goto cleanup;
+        tmp[i] = g_strdup(name);
     }
 
     ret = g_steal_pointer(&tmp);
@@ -81,10 +80,8 @@ virshNodeDeviceEventNameCompleter(vshControl *ctl G_GNUC_UNUSED,
     if (VIR_ALLOC_N(tmp, VIR_NODE_DEVICE_EVENT_ID_LAST + 1) < 0)
         return NULL;
 
-    for (i = 0; i < VIR_NODE_DEVICE_EVENT_ID_LAST; i++) {
-        if (VIR_STRDUP(tmp[i], virshNodeDeviceEventCallbacks[i].name) < 0)
-            return NULL;
-    }
+    for (i = 0; i < VIR_NODE_DEVICE_EVENT_ID_LAST; i++)
+        tmp[i] = g_strdup(virshNodeDeviceEventCallbacks[i].name);
 
     ret = g_steal_pointer(&tmp);
     return ret;
@@ -108,10 +105,8 @@ virshNodeDeviceCapabilityNameCompleter(vshControl *ctl,
     if (VIR_ALLOC_N(tmp, VIR_NODE_DEV_CAP_LAST + 1) < 0)
         return NULL;
 
-    for (i = 0; i < VIR_NODE_DEV_CAP_LAST; i++) {
-        if (VIR_STRDUP(tmp[i], virNodeDevCapTypeToString(i)) < 0)
-            return NULL;
-    }
+    for (i = 0; i < VIR_NODE_DEV_CAP_LAST; i++)
+        tmp[i] = g_strdup(virNodeDevCapTypeToString(i));
 
     return virshCommaStringListComplete(cap_str, (const char **)tmp);
 }
diff --git a/tools/virsh-completer-nwfilter.c b/tools/virsh-completer-nwfilter.c
index 5029becf09..989a363847 100644
--- a/tools/virsh-completer-nwfilter.c
+++ b/tools/virsh-completer-nwfilter.c
@@ -51,8 +51,7 @@ virshNWFilterNameCompleter(vshControl *ctl,
     for (i = 0; i < nnwfilters; i++) {
         const char *name = virNWFilterGetName(nwfilters[i]);
 
-        if (VIR_STRDUP(tmp[i], name) < 0)
-            goto cleanup;
+        tmp[i] = g_strdup(name);
     }
 
     ret = g_steal_pointer(&tmp);
@@ -91,8 +90,7 @@ virshNWFilterBindingNameCompleter(vshControl *ctl,
     for (i = 0; i < nbindings; i++) {
         const char *name = virNWFilterBindingGetPortDev(bindings[i]);
 
-        if (VIR_STRDUP(tmp[i], name) < 0)
-            goto cleanup;
+        tmp[i] = g_strdup(name);
     }
 
     ret = g_steal_pointer(&tmp);
diff --git a/tools/virsh-completer-pool.c b/tools/virsh-completer-pool.c
index ceb73eed06..bd9d9a9802 100644
--- a/tools/virsh-completer-pool.c
+++ b/tools/virsh-completer-pool.c
@@ -56,8 +56,7 @@ virshStoragePoolNameCompleter(vshControl *ctl,
     for (i = 0; i < npools; i++) {
         const char *name = virStoragePoolGetName(pools[i]);
 
-        if (VIR_STRDUP(tmp[i], name) < 0)
-            goto cleanup;
+        tmp[i] = g_strdup(name);
     }
 
     ret = g_steal_pointer(&tmp);
@@ -84,10 +83,8 @@ virshPoolEventNameCompleter(vshControl *ctl G_GNUC_UNUSED,
     if (VIR_ALLOC_N(tmp, VIR_STORAGE_POOL_EVENT_ID_LAST + 1) < 0)
         return NULL;
 
-    for (i = 0; i < VIR_STORAGE_POOL_EVENT_ID_LAST; i++) {
-        if (VIR_STRDUP(tmp[i], virshPoolEventCallbacks[i].name) < 0)
-            return NULL;
-    }
+    for (i = 0; i < VIR_STORAGE_POOL_EVENT_ID_LAST; i++)
+        tmp[i] = g_strdup(virshPoolEventCallbacks[i].name);
 
     ret = g_steal_pointer(&tmp);
     return ret;
@@ -111,10 +108,8 @@ virshPoolTypeCompleter(vshControl *ctl,
     if (VIR_ALLOC_N(tmp, VIR_STORAGE_POOL_LAST + 1) < 0)
         return NULL;
 
-    for (i = 0; i < VIR_STORAGE_POOL_LAST; i++) {
-        if (VIR_STRDUP(tmp[i], virStoragePoolTypeToString(i)) < 0)
-            return NULL;
-    }
+    for (i = 0; i < VIR_STORAGE_POOL_LAST; i++)
+        tmp[i] = g_strdup(virStoragePoolTypeToString(i));
 
     return virshCommaStringListComplete(type_str, (const char **)tmp);
 }
diff --git a/tools/virsh-completer-secret.c b/tools/virsh-completer-secret.c
index a533ac178f..bb02577c82 100644
--- a/tools/virsh-completer-secret.c
+++ b/tools/virsh-completer-secret.c
@@ -52,9 +52,9 @@ virshSecretUUIDCompleter(vshControl *ctl,
     for (i = 0; i < nsecrets; i++) {
         char uuid[VIR_UUID_STRING_BUFLEN];
 
-        if (virSecretGetUUIDString(secrets[i], uuid) < 0 ||
-            VIR_STRDUP(tmp[i], uuid) < 0)
+        if (virSecretGetUUIDString(secrets[i], uuid) < 0)
             goto cleanup;
+        tmp[i] = g_strdup(uuid);
     }
 
     ret = g_steal_pointer(&tmp);
@@ -81,10 +81,8 @@ virshSecretEventNameCompleter(vshControl *ctl G_GNUC_UNUSED,
     if (VIR_ALLOC_N(tmp, VIR_SECRET_EVENT_ID_LAST + 1) < 0)
         return NULL;
 
-    for (i = 0; i < VIR_SECRET_EVENT_ID_LAST; i++) {
-        if (VIR_STRDUP(tmp[i], virshSecretEventCallbacks[i].name) < 0)
-            return NULL;
-    }
+    for (i = 0; i < VIR_SECRET_EVENT_ID_LAST; i++)
+        tmp[i] = g_strdup(virshSecretEventCallbacks[i].name);
 
     ret = g_steal_pointer(&tmp);
     return ret;
diff --git a/tools/virsh-completer-snapshot.c b/tools/virsh-completer-snapshot.c
index 14bd7bc6e4..aa1135d132 100644
--- a/tools/virsh-completer-snapshot.c
+++ b/tools/virsh-completer-snapshot.c
@@ -58,8 +58,7 @@ virshSnapshotNameCompleter(vshControl *ctl,
     for (i = 0; i < nsnapshots; i++) {
         const char *name = virDomainSnapshotGetName(snapshots[i]);
 
-        if (VIR_STRDUP(tmp[i], name) < 0)
-            goto cleanup;
+        tmp[i] = g_strdup(name);
     }
 
     ret = g_steal_pointer(&tmp);
diff --git a/tools/virsh-completer-volume.c b/tools/virsh-completer-volume.c
index f14570f23d..ad54261534 100644
--- a/tools/virsh-completer-volume.c
+++ b/tools/virsh-completer-volume.c
@@ -58,8 +58,7 @@ virshStorageVolNameCompleter(vshControl *ctl,
     for (i = 0; i < nvols; i++) {
         const char *name = virStorageVolGetName(vols[i]);
 
-        if (VIR_STRDUP(tmp[i], name) < 0)
-            goto cleanup;
+        tmp[i] = g_strdup(name);
     }
 
     ret = g_steal_pointer(&tmp);
diff --git a/tools/virsh-completer.c b/tools/virsh-completer.c
index dd03c98a16..7e4bd895e5 100644
--- a/tools/virsh-completer.c
+++ b/tools/virsh-completer.c
@@ -100,8 +100,7 @@ virshCommaStringListComplete(const char *input,
     if (input) {
         char *comma = NULL;
 
-        if (VIR_STRDUP(inputCopy, input) < 0)
-            return NULL;
+        inputCopy = g_strdup(input);
 
         if ((comma = strrchr(inputCopy, ',')))
             *comma = '\0';
@@ -119,9 +118,10 @@ virshCommaStringListComplete(const char *input,
         if (virStringListHasString((const char **)inputList, options[i]))
             continue;
 
-        if ((inputCopy && virAsprintf(&ret[nret], "%s,%s", inputCopy, options[i]) < 0) ||
-            (!inputCopy && VIR_STRDUP(ret[nret], options[i]) < 0))
+        if (inputCopy && virAsprintf(&ret[nret], "%s,%s", inputCopy, options[i]) < 0)
             return NULL;
+        if (!inputCopy)
+            ret[nret] = g_strdup(options[i]);
 
         nret++;
     }
diff --git a/tools/virt-admin-completer.c b/tools/virt-admin-completer.c
index b8602efaf9..25dacf51d8 100644
--- a/tools/virt-admin-completer.c
+++ b/tools/virt-admin-completer.c
@@ -53,8 +53,7 @@ vshAdmServerCompleter(vshControl *ctl,
     for (i = 0; i < nsrvs; i++) {
         const char *name = virAdmServerGetName(srvs[i]);
 
-        if (VIR_STRDUP(ret[i], name) < 0)
-            goto error;
+        ret[i] = g_strdup(name);
 
         virAdmServerFree(srvs[i]);
     }
diff --git a/tools/virt-login-shell-helper.c b/tools/virt-login-shell-helper.c
index eb2b2dfe9e..ace8733968 100644
--- a/tools/virt-login-shell-helper.c
+++ b/tools/virt-login-shell-helper.c
@@ -102,10 +102,7 @@ static int virLoginShellGetShellArgv(virConfPtr conf,
     if (rv == 0) {
         if (VIR_ALLOC_N(*shargv, 2) < 0)
             return -1;
-        if (VIR_STRDUP((*shargv)[0], "/bin/sh") < 0) {
-            VIR_FREE(*shargv);
-            return -1;
-        }
+        (*shargv)[0] = g_strdup("/bin/sh");
         *shargvlen = 1;
     } else {
         *shargvlen = virStringListLength((const char *const *)shargv);
@@ -347,10 +344,8 @@ main(int argc, char **argv)
     if (cmdstr) {
         if (VIR_REALLOC_N(shargv, shargvlen + 3) < 0)
             goto cleanup;
-        if (VIR_STRDUP(shargv[shargvlen++], "-c") < 0)
-            goto cleanup;
-        if (VIR_STRDUP(shargv[shargvlen++], cmdstr) < 0)
-            goto cleanup;
+        shargv[shargvlen++] = g_strdup("-c");
+        shargv[shargvlen++] = g_strdup(cmdstr);
         shargv[shargvlen] = NULL;
     }
 
@@ -366,15 +361,13 @@ main(int argc, char **argv)
         goto cleanup;
     }
     tmp = strrchr(shcmd, '/');
-    if (VIR_STRDUP(shargv[0], tmp) < 0)
-        goto cleanup;
+    shargv[0] = g_strdup(tmp);
     shargv[0][0] = '-';
 
     /* We're duping the string because the clearenv()
      * call will shortly release the pointer we get
      * back from getenv() right here */
-    if (VIR_STRDUP(term, getenv("TERM")) < 0)
-        goto cleanup;
+    term = g_strdup(getenv("TERM"));
 
     /* A fork is required to create new process in correct pid namespace.  */
     if ((cpid = virFork()) < 0)
diff --git a/tools/vsh-table.c b/tools/vsh-table.c
index 8bd6d99778..c2271a4e2d 100644
--- a/tools/vsh-table.c
+++ b/tools/vsh-table.c
@@ -104,8 +104,7 @@ vshTableRowNew(const char *arg, va_list ap)
     while (arg) {
         char *tmp = NULL;
 
-        if (VIR_STRDUP(tmp, arg) < 0)
-            goto error;
+        tmp = g_strdup(arg);
 
         if (VIR_APPEND_ELEMENT(row->cells, row->ncells, tmp) < 0) {
             VIR_FREE(tmp);
diff --git a/tools/vsh.c b/tools/vsh.c
index a10a9625e4..3ef0031ee2 100644
--- a/tools/vsh.c
+++ b/tools/vsh.c
@@ -495,8 +495,7 @@ vshCmddefGetOption(vshControl *ctl, const vshCmdDef *cmd, const char *name,
                    opt->help = "string=value": treat boolean flag as
                    alias of option and its default value */
                 sa_assert(!alias);
-                if (VIR_STRDUP(alias, opt->help) < 0)
-                    goto cleanup;
+                alias = g_strdup(opt->help);
                 name = alias;
                 if ((value = strchr(name, '='))) {
                     *value = '\0';
@@ -506,8 +505,7 @@ vshCmddefGetOption(vshControl *ctl, const vshCmdDef *cmd, const char *name,
                                      opt->name);
                         goto cleanup;
                     }
-                    if (VIR_STRDUP(*optstr, value + 1) < 0)
-                        goto cleanup;
+                    *optstr = g_strdup(value + 1);
                 }
                 continue;
             }
@@ -3499,9 +3497,8 @@ cmdComplete(vshControl *ctl, const vshCmd *cmd)
 
     vshReadlineInit(ctl);
 
-    if (!(rl_line_buffer = virBufferContentAndReset(&buf)) &&
-        VIR_STRDUP(rl_line_buffer, "") < 0)
-        goto cleanup;
+    if (!(rl_line_buffer = virBufferContentAndReset(&buf)))
+        rl_line_buffer = g_strdup("");
 
     /* rl_point is current cursor position in rl_line_buffer.
      * In our case it's at the end of the whole line. */
-- 
2.21.0




More information about the libvir-list mailing list