[libvirt PATCH 05/12] tools: virsh: use automatic cleanup for xmlXPathContext

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


Signed-off-by: Ján Tomko <jtomko at redhat.com>
---
 tools/virsh-completer-domain.c |  6 ++----
 tools/virsh-domain-monitor.c   | 12 ++++--------
 tools/virsh-domain.c           | 27 +++++++++------------------
 tools/virsh-host.c             | 15 +++++----------
 tools/virsh-interface.c        |  6 ++----
 tools/virsh-volume.c           |  3 +--
 6 files changed, 23 insertions(+), 46 deletions(-)

diff --git a/tools/virsh-completer-domain.c b/tools/virsh-completer-domain.c
index d1430c951a..4cdc533a07 100644
--- a/tools/virsh-completer-domain.c
+++ b/tools/virsh-completer-domain.c
@@ -479,7 +479,7 @@ virshDomainVcpuCompleter(vshControl *ctl,
 {
     g_autoptr(virshDomain) dom = NULL;
     xmlDocPtr xml = NULL;
-    xmlXPathContextPtr ctxt = NULL;
+    g_autoptr(xmlXPathContext) ctxt = NULL;
     int nvcpus = 0;
     unsigned int id;
     char **ret = NULL;
@@ -506,7 +506,6 @@ virshDomainVcpuCompleter(vshControl *ctl,
     ret = g_steal_pointer(&tmp);
 
  cleanup:
-    xmlXPathFreeContext(ctxt);
     xmlFreeDoc(xml);
     return ret;
 }
@@ -519,7 +518,7 @@ virshDomainVcpulistCompleter(vshControl *ctl,
 {
     g_autoptr(virshDomain) dom = NULL;
     xmlDocPtr xml = NULL;
-    xmlXPathContextPtr ctxt = NULL;
+    g_autoptr(xmlXPathContext) ctxt = NULL;
     int nvcpus = 0;
     unsigned int id;
     g_auto(GStrv) vcpulist = NULL;
@@ -550,7 +549,6 @@ virshDomainVcpulistCompleter(vshControl *ctl,
     ret = virshCommaStringListComplete(vcpuid, (const char **)vcpulist);
 
  cleanup:
-    xmlXPathFreeContext(ctxt);
     xmlFreeDoc(xml);
     return ret;
 }
diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c
index d79f807d8b..cabf968393 100644
--- a/tools/virsh-domain-monitor.c
+++ b/tools/virsh-domain-monitor.c
@@ -56,7 +56,7 @@ virshGetDomainDescription(vshControl *ctl, virDomainPtr dom, bool title,
 {
     char *desc = NULL;
     xmlDocPtr doc = NULL;
-    xmlXPathContextPtr ctxt = NULL;
+    g_autoptr(xmlXPathContext) ctxt = NULL;
     int type;
 
     if (title)
@@ -92,7 +92,6 @@ virshGetDomainDescription(vshControl *ctl, virDomainPtr dom, bool title,
         desc = g_strdup("");
 
  cleanup:
-    xmlXPathFreeContext(ctxt);
     xmlFreeDoc(doc);
 
     return desc;
@@ -595,7 +594,7 @@ cmdDomblklist(vshControl *ctl, const vshCmd *cmd)
     bool ret = false;
     unsigned int flags = 0;
     xmlDocPtr xmldoc = NULL;
-    xmlXPathContextPtr ctxt = NULL;
+    g_autoptr(xmlXPathContext) ctxt = NULL;
     int ndisks;
     xmlNodePtr *disks = NULL;
     size_t i;
@@ -694,7 +693,6 @@ cmdDomblklist(vshControl *ctl, const vshCmd *cmd)
     VIR_FREE(device);
     VIR_FREE(type);
     VIR_FREE(disks);
-    xmlXPathFreeContext(ctxt);
     xmlFreeDoc(xmldoc);
     return ret;
 }
@@ -723,7 +721,7 @@ cmdDomiflist(vshControl *ctl, const vshCmd *cmd)
     bool ret = false;
     unsigned int flags = 0;
     xmlDocPtr xmldoc = NULL;
-    xmlXPathContextPtr ctxt = NULL;
+    g_autoptr(xmlXPathContext) ctxt = NULL;
     int ninterfaces;
     xmlNodePtr *interfaces = NULL;
     size_t i;
@@ -782,7 +780,6 @@ cmdDomiflist(vshControl *ctl, const vshCmd *cmd)
     vshTableFree(table);
     VIR_FREE(interfaces);
     xmlFreeDoc(xmldoc);
-    xmlXPathFreeContext(ctxt);
     return ret;
 }
 
@@ -824,7 +821,7 @@ cmdDomIfGetLink(vshControl *ctl, const vshCmd *cmd)
     virMacAddr macaddr;
     char macstr[VIR_MAC_STRING_BUFLEN] = "";
     xmlDocPtr xml = NULL;
-    xmlXPathContextPtr ctxt = NULL;
+    g_autoptr(xmlXPathContext) ctxt = NULL;
     xmlNodePtr *interfaces = NULL;
     int ninterfaces;
     unsigned int flags = 0;
@@ -877,7 +874,6 @@ cmdDomIfGetLink(vshControl *ctl, const vshCmd *cmd)
     VIR_FREE(state);
     VIR_FREE(interfaces);
     VIR_FREE(xpath);
-    xmlXPathFreeContext(ctxt);
     xmlFreeDoc(xml);
 
     return ret;
diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index b671ae398f..398190bb5b 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -3065,7 +3065,7 @@ cmdDomIfSetLink(vshControl *ctl, const vshCmd *cmd)
     unsigned int xmlflags = 0;
     size_t i;
     xmlDocPtr xml = NULL;
-    xmlXPathContextPtr ctxt = NULL;
+    g_autoptr(xmlXPathContext) ctxt = NULL;
     xmlXPathObjectPtr obj = NULL;
     xmlNodePtr cur = NULL;
     char *xml_buf = NULL;
@@ -3179,7 +3179,6 @@ cmdDomIfSetLink(vshControl *ctl, const vshCmd *cmd)
 
  cleanup:
     xmlXPathFreeObject(obj);
-    xmlXPathFreeContext(ctxt);
     xmlFreeDoc(xml);
     VIR_FREE(xml_buf);
 
@@ -3639,7 +3638,7 @@ cmdUndefine(vshControl *ctl, const vshCmd *cmd)
     virshUndefineVolume *vols = NULL; /* info about the volumes to delete */
     size_t nvols = 0;
     xmlDocPtr doc = NULL;
-    xmlXPathContextPtr ctxt = NULL;
+    g_autoptr(xmlXPathContext) ctxt = NULL;
     xmlNodePtr *vol_nodes = NULL;   /* XML nodes of volumes of the guest */
     int nvol_nodes;
     char *source = NULL;
@@ -3956,7 +3955,6 @@ cmdUndefine(vshControl *ctl, const vshCmd *cmd)
 
     VIR_FREE(vol_nodes);
     xmlFreeDoc(doc);
-    xmlXPathFreeContext(ctxt);
     return ret;
 
  error:
@@ -6555,7 +6553,7 @@ virshCPUCountCollect(vshControl *ctl,
     virDomainInfo info;
     int count;
     xmlDocPtr xml = NULL;
-    xmlXPathContextPtr ctxt = NULL;
+    g_autoptr(xmlXPathContext) ctxt = NULL;
 
     if (checkState &&
         ((flags & VIR_DOMAIN_AFFECT_LIVE && virDomainIsActive(dom) < 1) ||
@@ -6614,7 +6612,6 @@ virshCPUCountCollect(vshControl *ctl,
 
     ret = count;
  cleanup:
-    xmlXPathFreeContext(ctxt);
     xmlFreeDoc(xml);
 
     return ret;
@@ -6759,7 +6756,7 @@ virshDomainGetVcpuBitmap(vshControl *ctl,
     unsigned int flags = 0;
     virBitmap *ret = NULL;
     xmlDocPtr xml = NULL;
-    xmlXPathContextPtr ctxt = NULL;
+    g_autoptr(xmlXPathContext) ctxt = NULL;
     xmlNodePtr *nodes = NULL;
     int nnodes;
     size_t i;
@@ -6816,7 +6813,6 @@ virshDomainGetVcpuBitmap(vshControl *ctl,
  cleanup:
     VIR_FREE(online);
     VIR_FREE(nodes);
-    xmlXPathFreeContext(ctxt);
     xmlFreeDoc(xml);
     return ret;
 }
@@ -11389,7 +11385,7 @@ static bool
 cmdDomDisplay(vshControl *ctl, const vshCmd *cmd)
 {
     xmlDocPtr xml = NULL;
-    xmlXPathContextPtr ctxt = NULL;
+    g_autoptr(xmlXPathContext) ctxt = NULL;
     g_autoptr(virshDomain) dom = NULL;
     g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
     bool ret = false;
@@ -11618,7 +11614,6 @@ cmdDomDisplay(vshControl *ctl, const vshCmd *cmd)
     VIR_FREE(passwd);
     VIR_FREE(listen_addr);
     VIR_FREE(output);
-    xmlXPathFreeContext(ctxt);
     xmlFreeDoc(xml);
     return ret;
 }
@@ -11645,7 +11640,7 @@ static bool
 cmdVNCDisplay(vshControl *ctl, const vshCmd *cmd)
 {
     xmlDocPtr xml = NULL;
-    xmlXPathContextPtr ctxt = NULL;
+    g_autoptr(xmlXPathContext) ctxt = NULL;
     g_autoptr(virshDomain) dom = NULL;
     bool ret = false;
     int port = 0;
@@ -11693,7 +11688,6 @@ cmdVNCDisplay(vshControl *ctl, const vshCmd *cmd)
 
  cleanup:
     VIR_FREE(listen_addr);
-    xmlXPathFreeContext(ctxt);
     xmlFreeDoc(xml);
     return ret;
 }
@@ -11720,7 +11714,7 @@ static bool
 cmdTTYConsole(vshControl *ctl, const vshCmd *cmd)
 {
     xmlDocPtr xml = NULL;
-    xmlXPathContextPtr ctxt = NULL;
+    g_autoptr(xmlXPathContext) ctxt = NULL;
     bool ret = false;
     char *tty = NULL;
 
@@ -11734,7 +11728,6 @@ cmdTTYConsole(vshControl *ctl, const vshCmd *cmd)
     ret = true;
 
  cleanup:
-    xmlXPathFreeContext(ctxt);
     xmlFreeDoc(xml);
     VIR_FREE(tty);
     return ret;
@@ -12078,7 +12071,7 @@ virshDomainDetachInterface(char *doc,
 {
     xmlDocPtr xml = NULL;
     xmlXPathObjectPtr obj = NULL;
-    xmlXPathContextPtr ctxt = NULL;
+    g_autoptr(xmlXPathContext) ctxt = NULL;
     xmlNodePtr cur = NULL, matchNode = NULL;
     char *detach_xml = NULL;
     char buf[64];
@@ -12153,7 +12146,6 @@ virshDomainDetachInterface(char *doc,
     VIR_FREE(detach_xml);
     xmlFreeDoc(xml);
     xmlXPathFreeObject(obj);
-    xmlXPathFreeContext(ctxt);
     return ret == 0;
 }
 
@@ -12259,7 +12251,7 @@ virshFindDisk(const char *doc,
 {
     xmlDocPtr xml = NULL;
     xmlXPathObjectPtr obj = NULL;
-    xmlXPathContextPtr ctxt = NULL;
+    g_autoptr(xmlXPathContext) ctxt = NULL;
     xmlNodePtr cur = NULL;
     xmlNodePtr ret = NULL;
     size_t i;
@@ -12334,7 +12326,6 @@ virshFindDisk(const char *doc,
 
  cleanup:
     xmlXPathFreeObject(obj);
-    xmlXPathFreeContext(ctxt);
     xmlFreeDoc(xml);
     return ret;
 }
diff --git a/tools/virsh-host.c b/tools/virsh-host.c
index 7b47b7583d..b2d3a9c85f 100644
--- a/tools/virsh-host.c
+++ b/tools/virsh-host.c
@@ -168,7 +168,7 @@ cmdFreecell(vshControl *ctl, const vshCmd *cmd)
     size_t i;
     char *cap_xml = NULL;
     xmlDocPtr xml = NULL;
-    xmlXPathContextPtr ctxt = NULL;
+    g_autoptr(xmlXPathContext) ctxt = NULL;
     virshControl *priv = ctl->privData;
 
     VSH_EXCLUSIVE_OPTIONS_VAR(all, cellno);
@@ -243,7 +243,6 @@ cmdFreecell(vshControl *ctl, const vshCmd *cmd)
     ret = true;
 
  cleanup:
-    xmlXPathFreeContext(ctxt);
     xmlFreeDoc(xml);
     VIR_FREE(nodes);
     VIR_FREE(nodes_free);
@@ -308,7 +307,7 @@ cmdFreepages(vshControl *ctl, const vshCmd *cmd)
     int nodes_cnt;
     char *cap_xml = NULL;
     xmlDocPtr doc = NULL;
-    xmlXPathContextPtr ctxt = NULL;
+    g_autoptr(xmlXPathContext) ctxt = NULL;
     bool all = vshCommandOptBool(cmd, "all");
     bool cellno = vshCommandOptBool(cmd, "cellno");
     bool pagesz = vshCommandOptBool(cmd, "pagesize");
@@ -443,7 +442,6 @@ cmdFreepages(vshControl *ctl, const vshCmd *cmd)
 
     ret = true;
  cleanup:
-    xmlXPathFreeContext(ctxt);
     xmlFreeDoc(doc);
     VIR_FREE(cap_xml);
     VIR_FREE(nodes);
@@ -507,7 +505,7 @@ cmdAllocpages(vshControl *ctl, const vshCmd *cmd)
     unsigned int flags = 0;
     char *cap_xml = NULL;
     xmlDocPtr xml = NULL;
-    xmlXPathContextPtr ctxt = NULL;
+    g_autoptr(xmlXPathContext) ctxt = NULL;
     xmlNodePtr *nodes = NULL;
     virshControl *priv = ctl->privData;
 
@@ -571,7 +569,6 @@ cmdAllocpages(vshControl *ctl, const vshCmd *cmd)
 
     ret = true;
  cleanup:
-    xmlXPathFreeContext(ctxt);
     xmlFreeDoc(xml);
     VIR_FREE(nodes);
     VIR_FREE(cap_xml);
@@ -607,7 +604,7 @@ cmdMaxvcpus(vshControl *ctl, const vshCmd *cmd)
     int vcpus = -1;
     char *caps = NULL;
     xmlDocPtr xml = NULL;
-    xmlXPathContextPtr ctxt = NULL;
+    g_autoptr(xmlXPathContext) ctxt = NULL;
     virshControl *priv = ctl->privData;
     bool ret = false;
 
@@ -631,7 +628,6 @@ cmdMaxvcpus(vshControl *ctl, const vshCmd *cmd)
     ret = true;
 
  cleanup:
-    xmlXPathFreeContext(ctxt);
     xmlFreeDoc(xml);
     VIR_FREE(caps);
     return ret;
@@ -1125,7 +1121,7 @@ vshExtractCPUDefXMLs(vshControl *ctl,
     char *buffer = NULL;
     char *xmlStr = NULL;
     xmlDocPtr xml = NULL;
-    xmlXPathContextPtr ctxt = NULL;
+    g_autoptr(xmlXPathContext) ctxt = NULL;
     xmlNodePtr *nodes = NULL;
     char *doc;
     size_t i;
@@ -1188,7 +1184,6 @@ vshExtractCPUDefXMLs(vshControl *ctl,
     VIR_FREE(buffer);
     VIR_FREE(xmlStr);
     xmlFreeDoc(xml);
-    xmlXPathFreeContext(ctxt);
     VIR_FREE(nodes);
     return cpus;
 
diff --git a/tools/virsh-interface.c b/tools/virsh-interface.c
index 41acae5dcb..1a94eba1c3 100644
--- a/tools/virsh-interface.c
+++ b/tools/virsh-interface.c
@@ -822,7 +822,7 @@ cmdInterfaceBridge(vshControl *ctl, const vshCmd *cmd)
     xmlChar *br_xml = NULL;
     int br_xml_size;
     xmlDocPtr xml_doc = NULL;
-    xmlXPathContextPtr ctxt = NULL;
+    g_autoptr(xmlXPathContext) ctxt = NULL;
     xmlNodePtr top_node, br_node, if_node, cur;
     virshControl *priv = ctl->privData;
 
@@ -1001,7 +1001,6 @@ cmdInterfaceBridge(vshControl *ctl, const vshCmd *cmd)
     VIR_FREE(if_type);
     VIR_FREE(if2_name);
     VIR_FREE(delay_str);
-    xmlXPathFreeContext(ctxt);
     xmlFreeDoc(xml_doc);
     return ret;
 }
@@ -1044,7 +1043,7 @@ cmdInterfaceUnbridge(vshControl *ctl, const vshCmd *cmd)
     xmlChar *if_xml = NULL;
     int if_xml_size;
     xmlDocPtr xml_doc = NULL;
-    xmlXPathContextPtr ctxt = NULL;
+    g_autoptr(xmlXPathContext) ctxt = NULL;
     xmlNodePtr top_node, if_node, cur;
     virshControl *priv = ctl->privData;
 
@@ -1201,7 +1200,6 @@ cmdInterfaceUnbridge(vshControl *ctl, const vshCmd *cmd)
     VIR_FREE(br_xml);
     VIR_FREE(if_type);
     VIR_FREE(if_name);
-    xmlXPathFreeContext(ctxt);
     xmlFreeDoc(xml_doc);
     return ret;
 }
diff --git a/tools/virsh-volume.c b/tools/virsh-volume.c
index fe632b1b19..c9c4310cbd 100644
--- a/tools/virsh-volume.c
+++ b/tools/virsh-volume.c
@@ -526,7 +526,7 @@ static xmlChar *
 virshMakeCloneXML(const char *origxml, const char *newname)
 {
     xmlDocPtr doc = NULL;
-    xmlXPathContextPtr ctxt = NULL;
+    g_autoptr(xmlXPathContext) ctxt = NULL;
     xmlXPathObjectPtr obj = NULL;
     xmlChar *newxml = NULL;
     int size;
@@ -545,7 +545,6 @@ virshMakeCloneXML(const char *origxml, const char *newname)
 
  cleanup:
     xmlXPathFreeObject(obj);
-    xmlXPathFreeContext(ctxt);
     xmlFreeDoc(doc);
     return newxml;
 }
-- 
2.31.1




More information about the libvir-list mailing list