[libvirt PATCH 09/12] tools: virsh: reduce variable scope to use automatic cleanup

Ján Tomko jtomko at redhat.com
Thu Aug 12 08:32:47 UTC 2021


Some variables are used in a loop and only freed in the cleanup
section because we need to be able to jump out of the loop.

Reduce their scope and free them automatically.

Signed-off-by: Ján Tomko <jtomko at redhat.com>
---
 tools/virsh-domain-monitor.c | 39 +++++++++++++-----------------------
 tools/virsh-domain.c         | 34 +++++++++----------------------
 2 files changed, 23 insertions(+), 50 deletions(-)

diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index 4059acc7d6..b07959b1ee 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -598,10 +598,6 @@ cmdDomblklist(vshControl *ctl, const vshCmd *cmd)
     xmlNodePtr *disks = NULL;
     size_t i;
     bool details = false;
-    char *type = NULL;
-    char *device = NULL;
-    char *target = NULL;
-    char *source = NULL;
     g_autoptr(vshTable) table = NULL;
 
     if (vshCommandOptBool(cmd, "inactive"))
@@ -625,6 +621,11 @@ cmdDomblklist(vshControl *ctl, const vshCmd *cmd)
         goto cleanup;
 
     for (i = 0; i < ndisks; i++) {
+        g_autofree char *type = NULL;
+        g_autofree char *device = NULL;
+        g_autofree char *target = NULL;
+        g_autofree char *source = NULL;
+
         ctxt->node = disks[i];
 
         type = virXPathString("string(./@type)", ctxt);
@@ -674,11 +675,6 @@ cmdDomblklist(vshControl *ctl, const vshCmd *cmd)
                                   NULLSTR_MINUS(source), NULL) < 0)
                 goto cleanup;
         }
-
-        VIR_FREE(source);
-        VIR_FREE(target);
-        VIR_FREE(device);
-        VIR_FREE(type);
     }
 
     vshTablePrintToStdout(table, ctl);
@@ -686,10 +682,6 @@ cmdDomblklist(vshControl *ctl, const vshCmd *cmd)
     ret = true;
 
  cleanup:
-    VIR_FREE(source);
-    VIR_FREE(target);
-    VIR_FREE(device);
-    VIR_FREE(type);
     VIR_FREE(disks);
     return ret;
 }
@@ -999,7 +991,6 @@ cmdDomblkstat(vshControl *ctl, const vshCmd *cmd)
     virDomainBlockStatsStruct stats;
     virTypedParameterPtr params = NULL;
     virTypedParameterPtr par = NULL;
-    char *value = NULL;
     const char *field = NULL;
     int rc, nparams = 0;
     size_t i;
@@ -1065,6 +1056,8 @@ cmdDomblkstat(vshControl *ctl, const vshCmd *cmd)
 
         /* at first print all known values in desired order */
         for (i = 0; domblkstat_output[i].field != NULL; i++) {
+            g_autofree char *value = NULL;
+
             if (!(par = virTypedParamsGet(params, nparams,
                                           domblkstat_output[i].field)))
                 continue;
@@ -1087,18 +1080,17 @@ cmdDomblkstat(vshControl *ctl, const vshCmd *cmd)
 
             vshPrint(ctl, "%s %-*s %s\n", device,
                      human ? 31 : 0, field, value);
-
-            VIR_FREE(value);
         }
 
         /* go through the fields again, for remaining fields */
         for (i = 0; i < nparams; i++) {
+            g_autofree char *value = NULL;
+
             if (!*params[i].field)
                 continue;
 
             value = vshGetTypedParamValue(ctl, params+i);
             vshPrint(ctl, "%s %s %s\n", device, params[i].field, value);
-            VIR_FREE(value);
         }
     }
 
@@ -1939,7 +1931,6 @@ cmdList(vshControl *ctl, const vshCmd *cmd)
     bool optName = vshCommandOptBool(cmd, "name");
     bool optID = vshCommandOptBool(cmd, "id");
     size_t i;
-    char *title;
     char uuid[VIR_UUID_STRING_BUFLEN];
     int state;
     bool ret = false;
@@ -2022,6 +2013,8 @@ cmdList(vshControl *ctl, const vshCmd *cmd)
                 state = -2;
 
             if (optTitle) {
+                g_autofree char *title = NULL;
+
                 if (!(title = virshGetDomainDescription(ctl, dom, true, 0)))
                     goto cleanup;
                 if (vshTableRowAppend(table, id_buf,
@@ -2030,7 +2023,6 @@ cmdList(vshControl *ctl, const vshCmd *cmd)
                                       : virshDomainStateToString(state),
                                       title, NULL) < 0)
                     goto cleanup;
-                VIR_FREE(title);
             } else {
                 if (vshTableRowAppend(table, id_buf,
                                       virDomainGetName(dom),
@@ -2187,7 +2179,6 @@ virshDomainStatsPrintRecord(vshControl *ctl G_GNUC_UNUSED,
                             virDomainStatsRecordPtr record,
                             bool raw G_GNUC_UNUSED)
 {
-    char *param;
     size_t i;
 
     vshPrint(ctl, "Domain: '%s'\n", virDomainGetName(record->dom));
@@ -2195,12 +2186,12 @@ virshDomainStatsPrintRecord(vshControl *ctl G_GNUC_UNUSED,
     /* XXX: Implement pretty-printing */
 
     for (i = 0; i < record->nparams; i++) {
+        g_autofree char *param = NULL;
+
         if (!(param = vshGetTypedParamValue(ctl, record->params + i)))
             return false;
 
         vshPrint(ctl, "  %s=%s\n", record->params[i].field, param);
-
-        VIR_FREE(param);
     }
 
     return true;
@@ -2400,7 +2391,6 @@ cmdDomIfAddr(vshControl *ctl, const vshCmd *cmd)
 
     for (i = 0; i < ifaces_count; i++) {
         virDomainInterfacePtr iface = ifaces[i];
-        char *ip_addr_str = NULL;
         const char *type = NULL;
 
         if (ifacestr && STRNEQ(ifacestr, iface->name))
@@ -2416,6 +2406,7 @@ cmdDomIfAddr(vshControl *ctl, const vshCmd *cmd)
 
         for (j = 0; j < iface->naddrs; j++) {
             g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
+            g_autofree char *ip_addr_str = NULL;
 
             switch (iface->addrs[j].type) {
             case VIR_IP_ADDR_TYPE_IPV4:
@@ -2443,8 +2434,6 @@ cmdDomIfAddr(vshControl *ctl, const vshCmd *cmd)
             else
                 vshPrint(ctl, " %-10s %-17s    %s\n",
                          "-", "-", ip_addr_str);
-
-            VIR_FREE(ip_addr_str);
         }
     }
 
diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index f72ec36f6f..542e2a1736 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -3055,7 +3055,6 @@ cmdDomIfSetLink(vshControl *ctl, const vshCmd *cmd)
     g_autoptr(virshDomain) dom = NULL;
     const char *iface;
     const char *state;
-    char *value;
     virMacAddr macaddr;
     const char *element;
     const char *attr;
@@ -3119,13 +3118,10 @@ cmdDomIfSetLink(vshControl *ctl, const vshCmd *cmd)
         while (cur) {
             if (cur->type == XML_ELEMENT_NODE &&
                 virXMLNodeNameEqual(cur, element)) {
-                value = virXMLPropString(cur, attr);
+                g_autofree char *value = virXMLPropString(cur, attr);
 
-                if (STRCASEEQ(value, iface)) {
-                    VIR_FREE(value);
+                if (STRCASEEQ(value, iface))
                     goto hit;
-                }
-                VIR_FREE(value);
             }
             cur = cur->next;
         }
@@ -3639,9 +3635,6 @@ cmdUndefine(vshControl *ctl, const vshCmd *cmd)
     g_autoptr(xmlXPathContext) ctxt = NULL;
     xmlNodePtr *vol_nodes = NULL;   /* XML nodes of volumes of the guest */
     int nvol_nodes;
-    char *source = NULL;
-    char *target = NULL;
-    char *pool = NULL;
     size_t i;
     size_t j;
     virshControl *priv = ctl->privData;
@@ -3759,14 +3752,13 @@ cmdUndefine(vshControl *ctl, const vshCmd *cmd)
             goto error;
 
         for (i = 0; i < nvol_nodes; i++) {
+            g_autofree char *source = NULL;
+            g_autofree char *target = NULL;
+            g_autofree char *pool = NULL;
             virshUndefineVolume vol;
 
             ctxt->node = vol_nodes[i];
 
-            VIR_FREE(source);
-            VIR_FREE(target);
-            VIR_FREE(pool);
-
             /* get volume source and target paths */
             if (!(target = virXPathString("string(./target/@dev)", ctxt)))
                 goto error;
@@ -3936,9 +3928,6 @@ cmdUndefine(vshControl *ctl, const vshCmd *cmd)
     }
 
  cleanup:
-    VIR_FREE(source);
-    VIR_FREE(target);
-    VIR_FREE(pool);
     for (i = 0; i < nvols; i++) {
         VIR_FREE(vols[i].source);
         VIR_FREE(vols[i].target);
@@ -5096,7 +5085,6 @@ cmdSchedInfoUpdate(vshControl *ctl, const vshCmd *cmd,
                    virTypedParameterPtr src_params, int nsrc_params,
                    virTypedParameterPtr *update_params)
 {
-    char *set_field = NULL;
     char *set_val = NULL;
     const char *val = NULL;
     const vshCmdOpt *opt = NULL;
@@ -5107,7 +5095,8 @@ cmdSchedInfoUpdate(vshControl *ctl, const vshCmd *cmd,
     int rv;
 
     while ((opt = vshCommandOptArgv(ctl, cmd, opt))) {
-        set_field = g_strdup(opt->data);
+        g_autofree char *set_field = g_strdup(opt->data);
+
         if (!(set_val = strchr(set_field, '='))) {
             vshError(ctl, "%s", _("Invalid syntax for --set, "
                                   "expecting name=value"));
@@ -5121,8 +5110,6 @@ cmdSchedInfoUpdate(vshControl *ctl, const vshCmd *cmd,
                                   &params, &nparams, &maxparams,
                                   set_field, set_val) < 0)
             goto cleanup;
-
-        VIR_FREE(set_field);
     }
 
     rv = vshCommandOptStringReq(ctl, cmd, "cap", &val);
@@ -5145,7 +5132,6 @@ cmdSchedInfoUpdate(vshControl *ctl, const vshCmd *cmd,
     *update_params = g_steal_pointer(&params);
 
  cleanup:
-    VIR_FREE(set_field);
     virTypedParamsFree(params, nparams);
     return ret;
 }
@@ -6759,7 +6745,6 @@ virshDomainGetVcpuBitmap(vshControl *ctl,
     unsigned int curvcpus = 0;
     unsigned int maxvcpus = 0;
     unsigned int vcpuid;
-    char *online = NULL;
 
     if (inactive)
         flags |= VIR_DOMAIN_XML_INACTIVE;
@@ -6788,6 +6773,8 @@ virshDomainGetVcpuBitmap(vshControl *ctl,
     }
 
     for (i = 0; i < nnodes; i++) {
+        g_autofree char *online = NULL;
+
         ctxt->node = nodes[i];
 
         if (virXPathUInt("string(@id)", ctxt, &vcpuid) < 0 ||
@@ -6796,8 +6783,6 @@ virshDomainGetVcpuBitmap(vshControl *ctl,
 
         if (STREQ(online, "yes"))
             ignore_value(virBitmapSetBit(ret, vcpuid));
-
-        VIR_FREE(online);
     }
 
     if (virBitmapCountBits(ret) != curvcpus) {
@@ -6807,7 +6792,6 @@ virshDomainGetVcpuBitmap(vshControl *ctl,
     }
 
  cleanup:
-    VIR_FREE(online);
     VIR_FREE(nodes);
     return ret;
 }
-- 
2.31.1




More information about the libvir-list mailing list