[libvirt PATCH] tools: use g_autofree more

Ján Tomko jtomko at redhat.com
Wed Feb 1 15:11:08 UTC 2023


Remove some obvious uses of VIR_FREE in favor of automatic cleanup.

Signed-off-by: Ján Tomko <jtomko at redhat.com>
---
 tools/virsh-domain-monitor.c      |  5 +----
 tools/virsh-domain.c              | 37 +++++++++----------------------
 tools/virsh-network.c             |  6 ++---
 tools/virsh-nwfilter.c            |  3 +--
 tools/virt-admin.c                | 13 ++++-------
 tools/virt-host-validate-common.c |  6 ++---
 6 files changed, 20 insertions(+), 50 deletions(-)

diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index f89118f64f..c2134faba1 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -1213,7 +1213,6 @@ cmdDominfo(vshControl *ctl, const vshCmd *cmd)
     virDomainInfo info;
     g_autoptr(virshDomain) dom = NULL;
     virSecurityModel secmodel;
-    virSecurityLabelPtr seclabel;
     int persistent = 0;
     bool ret = true;
     int autostart;
@@ -1301,6 +1300,7 @@ cmdDominfo(vshControl *ctl, const vshCmd *cmd)
     } else {
         /* Only print something if a security model is active */
         if (secmodel.model[0] != '\0') {
+            g_autofree virSecurityLabelPtr seclabel = NULL;
             vshPrint(ctl, "%-15s %s\n", _("Security model:"), secmodel.model);
             vshPrint(ctl, "%-15s %s\n", _("Security DOI:"), secmodel.doi);
 
@@ -1308,15 +1308,12 @@ cmdDominfo(vshControl *ctl, const vshCmd *cmd)
             seclabel = g_new0(virSecurityLabel, 1);
 
             if (virDomainGetSecurityLabel(dom, seclabel) == -1) {
-                VIR_FREE(seclabel);
                 return false;
             } else {
                 if (seclabel->label[0] != '\0')
                     vshPrint(ctl, "%-15s %s (%s)\n", _("Security label:"),
                              seclabel->label, seclabel->enforcing ? "enforcing" : "permissive");
             }
-
-            VIR_FREE(seclabel);
         }
     }
 
diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index 59b2b3ce60..a4abcb1b03 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -366,7 +366,7 @@ cmdAttachDevice(vshControl *ctl, const vshCmd *cmd)
 {
     g_autoptr(virshDomain) dom = NULL;
     const char *from = NULL;
-    char *buffer;
+    g_autofree char *buffer = NULL;
     int rv;
     unsigned int flags = VIR_DOMAIN_AFFECT_CURRENT;
     bool current = vshCommandOptBool(cmd, "current");
@@ -404,8 +404,6 @@ cmdAttachDevice(vshControl *ctl, const vshCmd *cmd)
     else
         rv = virDomainAttachDevice(dom, buffer);
 
-    VIR_FREE(buffer);
-
     if (rv < 0) {
         vshError(ctl, _("Failed to attach device from %s"), from);
         return false;
@@ -2364,7 +2362,7 @@ cmdBlockcopy(vshControl *ctl, const vshCmd *cmd)
     const char *path = NULL;
     int abort_flags = 0;
     const char *xml = NULL;
-    char *xmlstr = NULL;
+    g_autofree char *xmlstr = NULL;
     bool print_xml = vshCommandOptBool(cmd, "print-xml");
     virTypedParameterPtr params = NULL;
     virshBlockJobWaitData *bjWait = NULL;
@@ -2567,7 +2565,6 @@ cmdBlockcopy(vshControl *ctl, const vshCmd *cmd)
     ret = true;
 
  cleanup:
-    VIR_FREE(xmlstr);
     virTypedParamsFree(params, nparams);
     virshBlockJobWaitFree(bjWait);
     return ret;
@@ -5624,7 +5621,7 @@ cmdScreenshot(vshControl *ctl, const vshCmd *cmd)
     bool ret = false;
     bool created = false;
     bool generated = false;
-    char *mime = NULL;
+    g_autofree char *mime = NULL;
     virshControl *priv = ctl->privData;
     virshStreamCallbackData cbdata;
 
@@ -5688,7 +5685,6 @@ cmdScreenshot(vshControl *ctl, const vshCmd *cmd)
         unlink(file);
     if (generated)
         VIR_FREE(file);
-    VIR_FREE(mime);
     return ret;
 }
 
@@ -7432,9 +7428,8 @@ cmdGuestvcpus(vshControl *ctl, const vshCmd *cmd)
             goto cleanup;
 
         for (i = 0; i < nparams; i++) {
-            char *str = vshGetTypedParamValue(ctl, &params[i]);
+            g_autofree char *str = vshGetTypedParamValue(ctl, &params[i]);
             vshPrint(ctl, "%-15s: %s\n", params[i].field, str);
-            VIR_FREE(str);
         }
     }
 
@@ -8305,7 +8300,7 @@ cmdDefine(vshControl *ctl, const vshCmd *cmd)
 {
     g_autoptr(virshDomain) dom = NULL;
     const char *from = NULL;
-    char *buffer;
+    g_autofree char *buffer = NULL;
     unsigned int flags = 0;
     virshControl *priv = ctl->privData;
 
@@ -8322,7 +8317,6 @@ cmdDefine(vshControl *ctl, const vshCmd *cmd)
         dom = virDomainDefineXMLFlags(priv->conn, buffer, flags);
     else
         dom = virDomainDefineXML(priv->conn, buffer);
-    VIR_FREE(buffer);
 
     if (!dom) {
         vshError(ctl, _("Failed to define domain from %s"), from);
@@ -10340,7 +10334,7 @@ cmdQemuAgentCommand(vshControl *ctl, const vshCmd *cmd)
 {
     g_autoptr(virshDomain) dom = NULL;
     bool ret = false;
-    char *guest_agent_cmd = NULL;
+    g_autofree char *guest_agent_cmd = NULL;
     char *result = NULL;
     int timeout = VIR_DOMAIN_QEMU_AGENT_COMMAND_DEFAULT;
     int judge = 0;
@@ -10405,7 +10399,6 @@ cmdQemuAgentCommand(vshControl *ctl, const vshCmd *cmd)
 
  cleanup:
     VIR_FREE(result);
-    VIR_FREE(guest_agent_cmd);
 
     return ret;
 }
@@ -11185,7 +11178,7 @@ doMigrate(void *opaque)
     if (vshCommandOptStringReq(ctl, cmd, "migrate-disks", &opt) < 0)
         goto out;
     if (opt) {
-        char **val = NULL;
+        g_autofree char **val = NULL;
 
         val = g_strsplit(opt, ",", 0);
 
@@ -11194,28 +11187,22 @@ doMigrate(void *opaque)
                                         &maxparams,
                                         VIR_MIGRATE_PARAM_MIGRATE_DISKS,
                                         (const char **)val) < 0) {
-            VIR_FREE(val);
             goto save_error;
         }
-
-        VIR_FREE(val);
     }
 
     if (vshCommandOptStringReq(ctl, cmd, "comp-methods", &opt) < 0)
         goto out;
     if (opt) {
-        char **val = g_strsplit(opt, ",", 0);
+        g_autofree char **val = g_strsplit(opt, ",", 0);
 
         if (virTypedParamsAddStringList(&params,
                                         &nparams,
                                         &maxparams,
                                         VIR_MIGRATE_PARAM_COMPRESSION,
                                         (const char **)val) < 0) {
-            VIR_FREE(val);
             goto save_error;
         }
-
-        VIR_FREE(val);
     }
 
     if ((rv = vshCommandOptInt(ctl, cmd, "comp-mt-level", &intOpt)) < 0) {
@@ -11257,7 +11244,7 @@ doMigrate(void *opaque)
     if (vshCommandOptStringReq(ctl, cmd, "xml", &opt) < 0)
         goto out;
     if (opt) {
-        char *xml;
+        g_autofree char *xml = NULL;
 
         if (virFileReadAll(opt, VSH_MAX_XML_FILE, &xml) < 0) {
             vshError(ctl, _("cannot read file '%s'"), opt);
@@ -11266,16 +11253,14 @@ doMigrate(void *opaque)
 
         if (virTypedParamsAddString(&params, &nparams, &maxparams,
                                     VIR_MIGRATE_PARAM_DEST_XML, xml) < 0) {
-            VIR_FREE(xml);
             goto save_error;
         }
-        VIR_FREE(xml);
     }
 
     if (vshCommandOptStringReq(ctl, cmd, "persistent-xml", &opt) < 0)
         goto out;
     if (opt) {
-        char *xml;
+        g_autofree char *xml = NULL;
 
         if (virFileReadAll(opt, VSH_MAX_XML_FILE, &xml) < 0) {
             vshError(ctl, _("cannot read file '%s'"), opt);
@@ -11284,10 +11269,8 @@ doMigrate(void *opaque)
 
         if (virTypedParamsAddString(&params, &nparams, &maxparams,
                                     VIR_MIGRATE_PARAM_PERSIST_XML, xml) < 0) {
-            VIR_FREE(xml);
             goto save_error;
         }
-        VIR_FREE(xml);
     }
 
     if ((rv = vshCommandOptInt(ctl, cmd, "auto-converge-initial", &intOpt)) < 0) {
diff --git a/tools/virsh-network.c b/tools/virsh-network.c
index 004719dad6..13f2e729eb 100644
--- a/tools/virsh-network.c
+++ b/tools/virsh-network.c
@@ -412,7 +412,7 @@ cmdNetworkInfo(vshControl *ctl, const vshCmd *cmd)
     int autostart;
     int persistent = -1;
     int active = -1;
-    char *bridge = NULL;
+    g_autofree char *bridge = NULL;
 
     if (!(network = virshCommandOptNetwork(ctl, cmd, NULL)))
         return false;
@@ -441,7 +441,6 @@ cmdNetworkInfo(vshControl *ctl, const vshCmd *cmd)
     if (bridge)
         vshPrint(ctl, "%-15s %s\n", _("Bridge:"), bridge);
 
-    VIR_FREE(bridge);
     return true;
 }
 
@@ -1500,7 +1499,7 @@ cmdNetworkPortCreate(vshControl *ctl, const vshCmd *cmd)
     virNetworkPortPtr port = NULL;
     const char *from = NULL;
     bool ret = false;
-    char *buffer = NULL;
+    g_autofree char *buffer = NULL;
     g_autoptr(virshNetwork) network = NULL;
     unsigned int flags = 0;
 
@@ -1533,7 +1532,6 @@ cmdNetworkPortCreate(vshControl *ctl, const vshCmd *cmd)
 
     ret = true;
  cleanup:
-    VIR_FREE(buffer);
     if (port)
         virNetworkPortFree(port);
     return ret;
diff --git a/tools/virsh-nwfilter.c b/tools/virsh-nwfilter.c
index d4112c8620..98380b452d 100644
--- a/tools/virsh-nwfilter.c
+++ b/tools/virsh-nwfilter.c
@@ -516,7 +516,7 @@ cmdNWFilterBindingCreate(vshControl *ctl, const vshCmd *cmd)
 {
     virNWFilterBindingPtr binding;
     const char *from = NULL;
-    char *buffer;
+    g_autofree char *buffer = NULL;
     unsigned int flags = 0;
     virshControl *priv = ctl->privData;
 
@@ -530,7 +530,6 @@ cmdNWFilterBindingCreate(vshControl *ctl, const vshCmd *cmd)
         return false;
 
     binding = virNWFilterBindingCreateXML(priv->conn, buffer, flags);
-    VIR_FREE(buffer);
 
     if (!binding) {
         vshError(ctl, _("Failed to create network filter from %s"), from);
diff --git a/tools/virt-admin.c b/tools/virt-admin.c
index 85b010b95f..8baf9636d3 100644
--- a/tools/virt-admin.c
+++ b/tools/virt-admin.c
@@ -73,7 +73,7 @@ vshAdmCatchDisconnect(virAdmConnectPtr conn G_GNUC_UNUSED,
     vshControl *ctl = opaque;
     const char *str = "unknown reason";
     virErrorPtr error;
-    char *uri = NULL;
+    g_autofree char *uri = NULL;
 
     if (reason == VIR_CONNECT_CLOSE_REASON_CLIENT)
         return;
@@ -97,8 +97,6 @@ vshAdmCatchDisconnect(virAdmConnectPtr conn G_GNUC_UNUSED,
     }
 
     vshError(ctl, _(str), NULLSTR(uri));
-    VIR_FREE(uri);
-
     virErrorRestore(&error);
 }
 
@@ -183,7 +181,7 @@ static const vshCmdInfo info_uri[] = {
 static bool
 cmdURI(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED)
 {
-    char *uri;
+    g_autofree char *uri = NULL;
     vshAdmControl *priv = ctl->privData;
 
     uri = virAdmConnectGetURI(priv->conn);
@@ -193,7 +191,6 @@ cmdURI(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED)
     }
 
     vshPrint(ctl, "%s\n", uri);
-    VIR_FREE(uri);
 
     return true;
 }
@@ -328,7 +325,7 @@ cmdSrvList(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED)
     int nsrvs = 0;
     size_t i;
     bool ret = false;
-    char *uri = NULL;
+    g_autofree char *uri = NULL;
     virAdmServerPtr *srvs = NULL;
     vshAdmControl *priv = ctl->privData;
     g_autoptr(vshTable) table = NULL;
@@ -365,7 +362,6 @@ cmdSrvList(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED)
             virAdmServerFree(srvs[i]);
         VIR_FREE(srvs);
     }
-    VIR_FREE(uri);
 
     return ret;
 }
@@ -702,9 +698,8 @@ cmdClientInfo(vshControl *ctl, const vshCmd *cmd)
              vshAdmClientTransportToString(virAdmClientGetTransport(clnt)));
 
     for (i = 0; i < nparams; i++) {
-        char *str = vshGetTypedParamValue(ctl, &params[i]);
+        g_autofree char *str = vshGetTypedParamValue(ctl, &params[i]);
         vshPrint(ctl, "%-15s: %s\n", params[i].field, str);
-        VIR_FREE(str);
     }
 
     ret = true;
diff --git a/tools/virt-host-validate-common.c b/tools/virt-host-validate-common.c
index 2ac96d1e19..a41bb346d2 100644
--- a/tools/virt-host-validate-common.c
+++ b/tools/virt-host-validate-common.c
@@ -56,7 +56,7 @@ void virHostMsgCheck(const char *prefix,
                      ...)
 {
     va_list args;
-    char *msg;
+    g_autofree char *msg = NULL;
 
     if (quiet)
         return;
@@ -66,7 +66,6 @@ void virHostMsgCheck(const char *prefix,
     va_end(args);
 
     fprintf(stdout, _("%6s: Checking %-60s: "), prefix, msg);
-    VIR_FREE(msg);
 }
 
 static bool virHostMsgWantEscape(void)
@@ -114,7 +113,7 @@ void virHostMsgFail(virHostValidateLevel level,
                     ...)
 {
     va_list args;
-    char *msg;
+    g_autofree char *msg = NULL;
 
     if (quiet)
         return;
@@ -129,7 +128,6 @@ void virHostMsgFail(virHostValidateLevel level,
     else
         fprintf(stdout, "%s (%s)\n",
                 _(failMessages[level]), msg);
-    VIR_FREE(msg);
 }
 
 
-- 
2.39.0



More information about the libvir-list mailing list