[PATCH] conf: convert network_conf.c to use g_auto* pointers

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This was mostly boilerplate conversion, but in one case I needed to
define several differently named char* to take the place of a single
char *tmp that was re-used multiple times, and in another place there
was a single char* that was used at the toplevel of the function, and
then later used repeatedly inside a for loop, so I defined a new
separate char* inside the loop.

Signed-off-by: Laine Stump <laine@xxxxxxxxxx>
---

This should be applied on top of Dan's IPv6 NAT patch series (it was
reviewing that series that showed me this file hadn't yet been
converted).

 src/conf/network_conf.c | 613 +++++++++++++++++-----------------------
 src/conf/network_conf.h |   1 +
 src/util/virxml.h       |   1 +
 3 files changed, 259 insertions(+), 356 deletions(-)

diff --git a/src/conf/network_conf.c b/src/conf/network_conf.c
index 1b89e2985d..ebe8a03262 100644
--- a/src/conf/network_conf.c
+++ b/src/conf/network_conf.c
@@ -306,8 +306,7 @@ virNetworkDefCopy(virNetworkDefPtr def,
                   virNetworkXMLOptionPtr xmlopt,
                   unsigned int flags)
 {
-    char *xml = NULL;
-    virNetworkDefPtr newDef = NULL;
+    g_autofree char *xml = NULL;
 
     if (!def) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
@@ -317,11 +316,9 @@ virNetworkDefCopy(virNetworkDefPtr def,
 
     /* deep copy with a format/parse cycle */
     if (!(xml = virNetworkDefFormat(def, xmlopt, flags)))
-        goto cleanup;
-    newDef = virNetworkDefParseString(xml, xmlopt);
- cleanup:
-    VIR_FREE(xml);
-    return newDef;
+       return NULL;
+
+    return virNetworkDefParseString(xml, xmlopt);
 }
 
 
@@ -465,48 +462,43 @@ virNetworkDHCPRangeDefParseXML(const char *networkName,
 {
     virSocketAddrRangePtr addr = &range->addr;
     xmlNodePtr cur = node->children;
-    char *start = NULL, *end = NULL;
-    int ret = -1;
+    g_autofree char *start = NULL;
+    g_autofree char *end = NULL;
 
     if (!(start = virXMLPropString(node, "start"))) {
         virReportError(VIR_ERR_XML_ERROR,
                        _("Missing 'start' attribute in dhcp range for network '%s'"),
                        networkName);
-        goto cleanup;
+        return -1;
     }
     if (virSocketAddrParse(&addr->start, start, AF_UNSPEC) < 0)
-        goto cleanup;
+        return -1;
 
     if (!(end = virXMLPropString(node, "end"))) {
         virReportError(VIR_ERR_XML_ERROR,
                        _("Missing 'end' attribute in dhcp range for network '%s'"),
                        networkName);
-        goto cleanup;
+        return -1;
     }
     if (virSocketAddrParse(&addr->end, end, AF_UNSPEC) < 0)
-        goto cleanup;
+        return -1;
 
     /* do a sanity check of the range */
     if (virSocketAddrGetRange(&addr->start, &addr->end, &ipdef->address,
                               virNetworkIPDefPrefix(ipdef)) < 0)
-        goto cleanup;
+        return -1;
 
     while (cur != NULL) {
         if (cur->type == XML_ELEMENT_NODE &&
             virXMLNodeNameEqual(cur, "lease")) {
 
             if (virNetworkDHCPLeaseTimeDefParseXML(&range->lease, cur) < 0)
-                goto cleanup;
+                return -1;
         }
         cur = cur->next;
     }
 
-    ret = 0;
-
- cleanup:
-    VIR_FREE(start);
-    VIR_FREE(end);
-    return ret;
+    return 0;
 }
 
 
@@ -517,11 +509,13 @@ virNetworkDHCPHostDefParseXML(const char *networkName,
                               virNetworkDHCPHostDefPtr host,
                               bool partialOkay)
 {
-    char *mac = NULL, *name = NULL, *ip = NULL, *id = NULL;
+    g_autofree char *mac = NULL;
+    g_autofree char *name = NULL;
+    g_autofree char *ip = NULL;
+    g_autofree char *id = NULL;
     virMacAddr addr;
     virSocketAddr inaddr;
     xmlNodePtr cur = node->children;
-    int ret = -1;
 
     mac = virXMLPropString(node, "mac");
     if (mac != NULL) {
@@ -530,20 +524,20 @@ virNetworkDHCPHostDefParseXML(const char *networkName,
                            _("Invalid to specify MAC address '%s' "
                              "in network '%s' IPv6 static host definition"),
                            mac, networkName);
-            goto cleanup;
+            return -1;
         }
         if (virMacAddrParse(mac, &addr) < 0) {
             virReportError(VIR_ERR_XML_ERROR,
                            _("Cannot parse MAC address '%s' in network '%s'"),
                            mac, networkName);
-            goto cleanup;
+            return -1;
         }
         if (virMacAddrIsMulticast(&addr)) {
             virReportError(VIR_ERR_XML_ERROR,
                            _("expected unicast mac address, found "
                              "multicast '%s' in network '%s'"),
                            (const char *)mac, networkName);
-            goto cleanup;
+            return -1;
         }
     }
 
@@ -554,7 +548,7 @@ virNetworkDHCPHostDefParseXML(const char *networkName,
             virReportError(VIR_ERR_XML_ERROR,
                            _("Invalid character '%c' in id '%s' of network '%s'"),
                            *cp, id, networkName);
-            goto cleanup;
+            return -1;
         }
     }
 
@@ -563,7 +557,7 @@ virNetworkDHCPHostDefParseXML(const char *networkName,
         virReportError(VIR_ERR_XML_ERROR,
                        _("Cannot use host name '%s' in network '%s'"),
                        name, networkName);
-        goto cleanup;
+        return -1;
     }
 
     ip = virXMLPropString(node, "ip");
@@ -572,7 +566,7 @@ virNetworkDHCPHostDefParseXML(const char *networkName,
                        _("Invalid IP address in static host definition "
                          "for network '%s'"),
                        networkName);
-        goto cleanup;
+        return -1;
     }
 
     if (partialOkay) {
@@ -583,7 +577,7 @@ virNetworkDHCPHostDefParseXML(const char *networkName,
                              "must be specified for static host definition "
                              "in network '%s' "),
                            networkName);
-            goto cleanup;
+            return -1;
         }
     } else {
         /* normal usage - you need at least name (IPv6) or one of MAC
@@ -595,21 +589,21 @@ virNetworkDHCPHostDefParseXML(const char *networkName,
                            _("Static host definition in IPv6 network '%s' "
                              "must have id or name attribute"),
                            networkName);
-                goto cleanup;
+                return -1;
             }
         } else if (!(mac || name)) {
             virReportError(VIR_ERR_XML_ERROR,
                            _("Static host definition in IPv4 network '%s' "
                              "must have mac or name attribute"),
                            networkName);
-            goto cleanup;
+            return -1;
         }
         if (!ip) {
             virReportError(VIR_ERR_XML_ERROR,
                            _("Missing IP address in static host definition "
                              "for network '%s'"),
                            networkName);
-            goto cleanup;
+            return -1;
         }
     }
 
@@ -618,7 +612,7 @@ virNetworkDHCPHostDefParseXML(const char *networkName,
             virXMLNodeNameEqual(cur, "lease")) {
 
             if (virNetworkDHCPLeaseTimeDefParseXML(&host->lease, cur) < 0)
-                goto cleanup;
+                return -1;
         }
         cur = cur->next;
     }
@@ -631,14 +625,9 @@ virNetworkDHCPHostDefParseXML(const char *networkName,
     name = NULL;
     if (ip)
         host->ip = inaddr;
-    ret = 0;
 
- cleanup:
-    VIR_FREE(mac);
-    VIR_FREE(id);
-    VIR_FREE(name);
-    VIR_FREE(ip);
-    return ret;
+    return 0;
+
 }
 
 
@@ -676,8 +665,8 @@ virNetworkDHCPDefParseXML(const char *networkName,
         } else if (VIR_SOCKET_ADDR_IS_FAMILY(&def->address, AF_INET) &&
                    cur->type == XML_ELEMENT_NODE &&
                    virXMLNodeNameEqual(cur, "bootp")) {
-            char *file;
-            char *server;
+            g_autofree char *file = NULL;
+            g_autofree char *server = NULL;
             virSocketAddr inaddr;
             memset(&inaddr, 0, sizeof(inaddr));
 
@@ -689,14 +678,12 @@ virNetworkDHCPDefParseXML(const char *networkName,
 
             if (server &&
                 virSocketAddrParse(&inaddr, server, AF_UNSPEC) < 0) {
-                VIR_FREE(file);
-                VIR_FREE(server);
                 goto cleanup;
             }
 
             def->bootfile = file;
+            file = NULL;
             def->bootserver = inaddr;
-            VIR_FREE(server);
         }
 
         cur = cur->next;
@@ -716,7 +703,7 @@ virNetworkDNSHostDefParseXML(const char *networkName,
                              bool partialOkay)
 {
     xmlNodePtr cur;
-    char *ip;
+    g_autofree char *ip = NULL;
 
     if (!(ip = virXMLPropString(node, "ip")) && !partialOkay) {
         virReportError(VIR_ERR_XML_DETAIL,
@@ -729,17 +716,15 @@ virNetworkDNSHostDefParseXML(const char *networkName,
         virReportError(VIR_ERR_XML_DETAIL,
                        _("Invalid IP address in network '%s' DNS HOST record"),
                        networkName);
-        VIR_FREE(ip);
         goto error;
     }
-    VIR_FREE(ip);
 
     cur = node->children;
     while (cur != NULL) {
         if (cur->type == XML_ELEMENT_NODE &&
             virXMLNodeNameEqual(cur, "hostname")) {
               if (cur->children != NULL) {
-                  char *name = (char *) xmlNodeGetContent(cur);
+                  g_autofree char *name = (char *) xmlNodeGetContent(cur);
 
                   if (!name) {
                       virReportError(VIR_ERR_XML_DETAIL,
@@ -747,10 +732,8 @@ virNetworkDNSHostDefParseXML(const char *networkName,
                                      networkName);
                       goto error;
                   }
-                  if (VIR_APPEND_ELEMENT(def->names, def->nnames, name) < 0) {
-                      VIR_FREE(name);
+                  if (VIR_APPEND_ELEMENT(def->names, def->nnames, name) < 0)
                       goto error;
-                  }
               }
         }
         cur = cur->next;
@@ -950,15 +933,14 @@ virNetworkDNSDefParseXML(const char *networkName,
                          xmlXPathContextPtr ctxt,
                          virNetworkDNSDefPtr def)
 {
-    xmlNodePtr *hostNodes = NULL;
-    xmlNodePtr *srvNodes = NULL;
-    xmlNodePtr *txtNodes = NULL;
-    xmlNodePtr *fwdNodes = NULL;
-    char *forwardPlainNames = NULL;
-    char *enable = NULL;
+    g_autofree xmlNodePtr *hostNodes = NULL;
+    g_autofree xmlNodePtr *srvNodes = NULL;
+    g_autofree xmlNodePtr *txtNodes = NULL;
+    g_autofree xmlNodePtr *fwdNodes = NULL;
+    g_autofree char *forwardPlainNames = NULL;
+    g_autofree char *enable = NULL;
     int nhosts, nsrvs, ntxts, nfwds;
     size_t i;
-    int ret = -1;
     VIR_XPATH_NODE_AUTORESTORE(ctxt);
 
     ctxt->node = node;
@@ -971,7 +953,7 @@ virNetworkDNSDefParseXML(const char *networkName,
                            _("Invalid dns enable setting '%s' "
                              "in network '%s'"),
                            enable, networkName);
-            goto cleanup;
+            return -1;
         }
     }
 
@@ -983,7 +965,7 @@ virNetworkDNSDefParseXML(const char *networkName,
                            _("Invalid dns forwardPlainNames setting '%s' "
                              "in network '%s'"),
                            forwardPlainNames, networkName);
-            goto cleanup;
+            return -1;
         }
     }
 
@@ -992,14 +974,14 @@ virNetworkDNSDefParseXML(const char *networkName,
         virReportError(VIR_ERR_XML_ERROR,
                        _("invalid <forwarder> element found in <dns> of network %s"),
                        networkName);
-        goto cleanup;
+        return -1;
     }
     if (nfwds > 0) {
         if (VIR_ALLOC_N(def->forwarders, nfwds) < 0)
-            goto cleanup;
+            return -1;
 
         for (i = 0; i < nfwds; i++) {
-            char *addr = virXMLPropString(fwdNodes[i], "addr");
+            g_autofree char *addr = virXMLPropString(fwdNodes[i], "addr");
 
             if (addr && virSocketAddrParse(&def->forwarders[i].addr,
                                            addr, AF_UNSPEC) < 0) {
@@ -1007,17 +989,15 @@ virNetworkDNSDefParseXML(const char *networkName,
                                _("Invalid forwarder IP address '%s' "
                                  "in network '%s'"),
                                addr, networkName);
-                VIR_FREE(addr);
-                goto cleanup;
+                return -1;
             }
             def->forwarders[i].domain = virXMLPropString(fwdNodes[i], "domain");
             if (!(addr || def->forwarders[i].domain)) {
                 virReportError(VIR_ERR_XML_ERROR, "%s",
                                _("Invalid forwarder element, must contain "
                                  "at least one of addr or domain"));
-                goto cleanup;
+                return -1;
             }
-            VIR_FREE(addr);
             def->nfwds++;
         }
     }
@@ -1027,16 +1007,16 @@ virNetworkDNSDefParseXML(const char *networkName,
         virReportError(VIR_ERR_XML_ERROR,
                        _("invalid <host> element found in <dns> of network %s"),
                        networkName);
-        goto cleanup;
+        return -1;
     }
     if (nhosts > 0) {
         if (VIR_ALLOC_N(def->hosts, nhosts) < 0)
-            goto cleanup;
+            return -1;
 
         for (i = 0; i < nhosts; i++) {
             if (virNetworkDNSHostDefParseXML(networkName, hostNodes[i],
                                              &def->hosts[def->nhosts], false) < 0) {
-                goto cleanup;
+                return -1;
             }
             def->nhosts++;
         }
@@ -1047,16 +1027,16 @@ virNetworkDNSDefParseXML(const char *networkName,
         virReportError(VIR_ERR_XML_ERROR,
                        _("invalid <srv> element found in <dns> of network %s"),
                        networkName);
-        goto cleanup;
+        return -1;
     }
     if (nsrvs > 0) {
         if (VIR_ALLOC_N(def->srvs, nsrvs) < 0)
-            goto cleanup;
+            return -1;
 
         for (i = 0; i < nsrvs; i++) {
             if (virNetworkDNSSrvDefParseXML(networkName, srvNodes[i], ctxt,
                                             &def->srvs[def->nsrvs], false) < 0) {
-                goto cleanup;
+                return -1;
             }
             def->nsrvs++;
         }
@@ -1067,16 +1047,16 @@ virNetworkDNSDefParseXML(const char *networkName,
         virReportError(VIR_ERR_XML_ERROR,
                        _("invalid <txt> element found in <dns> of network %s"),
                        networkName);
-        goto cleanup;
+        return -1;
     }
     if (ntxts > 0) {
         if (VIR_ALLOC_N(def->txts, ntxts) < 0)
-            goto cleanup;
+            return -1;
 
         for (i = 0; i < ntxts; i++) {
             if (virNetworkDNSTxtDefParseXML(networkName, txtNodes[i],
                                             &def->txts[def->ntxts], false) < 0) {
-                goto cleanup;
+                return -1;
             }
             def->ntxts++;
         }
@@ -1087,18 +1067,10 @@ virNetworkDNSDefParseXML(const char *networkName,
         virReportError(VIR_ERR_XML_ERROR,
                        _("Extra data in disabled network '%s'"),
                        networkName);
-        goto cleanup;
+        return -1;
     }
 
-    ret = 0;
- cleanup:
-    VIR_FREE(enable);
-    VIR_FREE(forwardPlainNames);
-    VIR_FREE(fwdNodes);
-    VIR_FREE(hostNodes);
-    VIR_FREE(srvNodes);
-    VIR_FREE(txtNodes);
-    return ret;
+    return 0;
 }
 
 
@@ -1115,11 +1087,12 @@ virNetworkIPDefParseXML(const char *networkName,
 
     VIR_XPATH_NODE_AUTORESTORE(ctxt);
     xmlNodePtr dhcp;
-    char *address = NULL, *netmask = NULL;
+    g_autofree char *address = NULL;
+    g_autofree char *netmask = NULL;
+    g_autofree char *localPtr = NULL;
     unsigned long prefix = 0;
     int prefixRc;
-    int result = -1;
-    char *localPtr = NULL;
+    int ret = -1;
 
     ctxt->node = node;
 
@@ -1243,16 +1216,13 @@ virNetworkIPDefParseXML(const char *networkName,
         def->tftproot = virXPathString("string(./tftp[1]/@root)", ctxt);
     }
 
-    result = 0;
+    ret = 0;
 
  cleanup:
-    if (result < 0)
+    if (ret < 0)
         virNetworkIPDefClear(def);
-    VIR_FREE(address);
-    VIR_FREE(netmask);
-    VIR_FREE(localPtr);
 
-    return result;
+    return ret;
 }
 
 
@@ -1290,10 +1260,10 @@ virNetworkPortGroupParseXML(virPortGroupDefPtr def,
     xmlNodePtr virtPortNode;
     xmlNodePtr vlanNode;
     xmlNodePtr bandwidth_node;
-    char *isDefault = NULL;
-    char *trustGuestRxFilters = NULL;
+    g_autofree char *isDefault = NULL;
+    g_autofree char *trustGuestRxFilters = NULL;
 
-    int result = -1;
+    int ret = -1;
 
     ctxt->node = node;
 
@@ -1335,14 +1305,12 @@ virNetworkPortGroupParseXML(virPortGroupDefPtr def,
     if (vlanNode && virNetDevVlanParse(vlanNode, ctxt, &def->vlan) < 0)
         goto cleanup;
 
-    result = 0;
+    ret = 0;
  cleanup:
-    if (result < 0)
+    if (ret < 0)
         virPortGroupDefClear(def);
-    VIR_FREE(isDefault);
-    VIR_FREE(trustGuestRxFilters);
 
-    return result;
+    return ret;
 }
 
 
@@ -1352,13 +1320,12 @@ virNetworkForwardNatDefParseXML(const char *networkName,
                                 xmlXPathContextPtr ctxt,
                                 virNetworkForwardDefPtr def)
 {
-    int ret = -1;
-    xmlNodePtr *natAddrNodes = NULL;
-    xmlNodePtr *natPortNodes = NULL;
     int nNatAddrs, nNatPorts;
-    char *addrStart = NULL;
-    char *addrEnd = NULL;
-    char *ipv6 = NULL;
+    g_autofree xmlNodePtr *natAddrNodes = NULL;
+    g_autofree xmlNodePtr *natPortNodes = NULL;
+    g_autofree char *addrStart = NULL;
+    g_autofree char *addrEnd = NULL;
+    g_autofree char *ipv6 = NULL;
     VIR_XPATH_NODE_AUTORESTORE(ctxt);
 
     ctxt->node = node;
@@ -1367,7 +1334,7 @@ virNetworkForwardNatDefParseXML(const char *networkName,
         virReportError(VIR_ERR_XML_ERROR,
                        _("The <nat> element can only be used when <forward> 'mode' is 'nat' in network %s"),
                        networkName);
-        goto cleanup;
+        return -1;
     }
 
     ipv6 = virXMLPropString(node, "ipv6");
@@ -1378,10 +1345,9 @@ virNetworkForwardNatDefParseXML(const char *networkName,
                            _("Invalid ipv6 setting '%s' "
                              "in network '%s' NAT"),
                            ipv6, networkName);
-            goto cleanup;
+            return -1;
         }
         def->natIPv6 = natIPv6;
-        VIR_FREE(ipv6);
     }
 
     /* addresses for SNAT */
@@ -1390,26 +1356,26 @@ virNetworkForwardNatDefParseXML(const char *networkName,
         virReportError(VIR_ERR_XML_ERROR,
                        _("invalid <address> element found in <forward> of "
                          "network %s"), networkName);
-        goto cleanup;
+        return -1;
     } else if (nNatAddrs > 1) {
         virReportError(VIR_ERR_XML_ERROR,
                        _("Only one <address> element is allowed in <nat> in "
                          "<forward> in network %s"), networkName);
-        goto cleanup;
+        return -1;
     } else if (nNatAddrs == 1) {
         addrStart = virXMLPropString(*natAddrNodes, "start");
         if (addrStart == NULL) {
             virReportError(VIR_ERR_XML_ERROR,
                            _("missing 'start' attribute in <address> element in <nat> in "
                              "<forward> in network %s"), networkName);
-            goto cleanup;
+            return -1;
         }
         addrEnd = virXMLPropString(*natAddrNodes, "end");
         if (addrEnd == NULL) {
             virReportError(VIR_ERR_XML_ERROR,
                            _("missing 'end' attribute in <address> element in <nat> in "
                              "<forward> in network %s"), networkName);
-            goto cleanup;
+            return -1;
         }
     }
 
@@ -1417,34 +1383,34 @@ virNetworkForwardNatDefParseXML(const char *networkName,
         virReportError(VIR_ERR_XML_ERROR,
                        _("Bad ipv4 start address '%s' in <nat> in <forward> in "
                          "network '%s'"), addrStart, networkName);
-        goto cleanup;
+        return -1;
     }
 
     if (addrEnd && virSocketAddrParse(&def->addr.end, addrEnd, AF_INET) < 0) {
         virReportError(VIR_ERR_XML_ERROR,
                        _("Bad ipv4 end address '%s' in <nat> in <forward> in "
                          "network '%s'"), addrEnd, networkName);
-        goto cleanup;
+        return -1;
     }
 
     if (addrStart && addrEnd) {
         /* verify that start <= end */
         if (virSocketAddrGetRange(&def->addr.start, &def->addr.end, NULL, 0) < 0)
-            goto cleanup;
+            return -1;
     } else {
         if (addrStart) {
             virReportError(VIR_ERR_XML_ERROR,
                            _("Only start address '%s' specified in <nat> in "
                              "<forward> in network '%s'"),
                            addrStart, networkName);
-            goto cleanup;
+            return -1;
         }
         if (addrEnd) {
             virReportError(VIR_ERR_XML_ERROR,
                            _("Only end address '%s' specified in <nat> in "
                              "<forward> in network '%s'"),
                            addrEnd, networkName);
-            goto cleanup;
+            return -1;
         }
     }
 
@@ -1454,12 +1420,12 @@ virNetworkForwardNatDefParseXML(const char *networkName,
         virReportError(VIR_ERR_XML_ERROR,
                        _("invalid <port> element found in <forward> of "
                          "network %s"), networkName);
-        goto cleanup;
+        return -1;
     } else if (nNatPorts > 1) {
         virReportError(VIR_ERR_XML_ERROR,
                        _("Only one <port> element is allowed in <nat> in "
                          "<forward> in network %s"), networkName);
-        goto cleanup;
+        return -1;
     } else if (nNatPorts == 1) {
         if (virXPathUInt("string(./port[1]/@start)", ctxt, &def->port.start) < 0
             || def->port.start > 65535) {
@@ -1468,24 +1434,17 @@ virNetworkForwardNatDefParseXML(const char *networkName,
                            _("Missing or invalid 'start' attribute in <port> "
                              "in <nat> in <forward> in network %s"),
                              networkName);
-            goto cleanup;
+            return -1;
         }
         if (virXPathUInt("string(./port[1]/@end)", ctxt, &def->port.end) < 0
             || def->port.end > 65535 || def->port.end < def->port.start) {
             virReportError(VIR_ERR_XML_DETAIL,
                            _("Missing or invalid 'end' attribute in <port> in "
                              "<nat> in <forward> in network %s"), networkName);
-            goto cleanup;
+            return -1;
         }
     }
-    ret = 0;
-
- cleanup:
-    VIR_FREE(addrStart);
-    VIR_FREE(addrEnd);
-    VIR_FREE(natAddrNodes);
-    VIR_FREE(natPortNodes);
-    return ret;
+    return 0;
 }
 
 
@@ -1496,16 +1455,15 @@ virNetworkForwardDefParseXML(const char *networkName,
                              virNetworkForwardDefPtr def)
 {
     size_t i, j;
-    int ret = -1;
-    xmlNodePtr *forwardIfNodes = NULL;
-    xmlNodePtr *forwardPfNodes = NULL;
-    xmlNodePtr *forwardAddrNodes = NULL;
-    xmlNodePtr *forwardNatNodes = NULL;
     int nForwardIfs, nForwardAddrs, nForwardPfs, nForwardNats;
-    char *forwardDev = NULL;
-    char *forwardManaged = NULL;
-    char *forwardDriverName = NULL;
-    char *type = NULL;
+    g_autofree xmlNodePtr *forwardIfNodes = NULL;
+    g_autofree xmlNodePtr *forwardPfNodes = NULL;
+    g_autofree xmlNodePtr *forwardAddrNodes = NULL;
+    g_autofree xmlNodePtr *forwardNatNodes = NULL;
+    g_autofree char *forwardDev = NULL;
+    g_autofree char *forwardManaged = NULL;
+    g_autofree char *forwardDriverName = NULL;
+    g_autofree char *type = NULL;
     VIR_XPATH_NODE_AUTORESTORE(ctxt);
 
     ctxt->node = node;
@@ -1516,9 +1474,8 @@ virNetworkForwardDefParseXML(const char *networkName,
         if ((def->type = virNetworkForwardTypeFromString(type)) < 0) {
             virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                            _("unknown forwarding type '%s'"), type);
-            goto cleanup;
+            return -1;
         }
-        VIR_FREE(type);
     }
 
     forwardManaged = virXPathString("string(./@managed)", ctxt);
@@ -1537,7 +1494,7 @@ virNetworkForwardDefParseXML(const char *networkName,
                            _("Unknown forward <driver name='%s'/> "
                              "in network %s"),
                            forwardDriverName, networkName);
-            goto cleanup;
+            return -1;
         }
         def->driverName = driverName;
     }
@@ -1548,7 +1505,7 @@ virNetworkForwardDefParseXML(const char *networkName,
         virReportError(VIR_ERR_XML_ERROR,
                        _("invalid <interface> element found in <forward> of network %s"),
                        networkName);
-        goto cleanup;
+        return -1;
     }
 
     nForwardAddrs = virXPathNodeSet("./address", ctxt, &forwardAddrNodes);
@@ -1556,7 +1513,7 @@ virNetworkForwardDefParseXML(const char *networkName,
         virReportError(VIR_ERR_XML_ERROR,
                        _("invalid <address> element found in <forward> of network %s"),
                        networkName);
-        goto cleanup;
+        return -1;
     }
 
     nForwardPfs = virXPathNodeSet("./pf", ctxt, &forwardPfNodes);
@@ -1564,7 +1521,7 @@ virNetworkForwardDefParseXML(const char *networkName,
         virReportError(VIR_ERR_XML_ERROR,
                        _("invalid <pf> element found in <forward> of network %s"),
                        networkName);
-        goto cleanup;
+        return -1;
     }
 
     nForwardNats = virXPathNodeSet("./nat", ctxt, &forwardNatNodes);
@@ -1572,17 +1529,17 @@ virNetworkForwardDefParseXML(const char *networkName,
         virReportError(VIR_ERR_XML_ERROR,
                        _("invalid <nat> element found in <forward> of network %s"),
                        networkName);
-        goto cleanup;
+        return -1;
     } else if (nForwardNats > 1) {
         virReportError(VIR_ERR_XML_ERROR,
                        _("Only one <nat> element is allowed in <forward> of network %s"),
                        networkName);
-        goto cleanup;
+        return -1;
     } else if (nForwardNats == 1) {
         if (virNetworkForwardNatDefParseXML(networkName,
                                             *forwardNatNodes,
                                             ctxt, def) < 0)
-            goto cleanup;
+            return -1;
     }
 
     forwardDev = virXPathString("string(./@dev)", ctxt);
@@ -1591,12 +1548,12 @@ virNetworkForwardDefParseXML(const char *networkName,
                        _("the <forward> 'dev' attribute cannot be used when "
                          "<address> or <pf> sub-elements are present "
                          "in network %s"));
-        goto cleanup;
+        return -1;
     }
 
     if (nForwardIfs > 0 || forwardDev) {
         if (VIR_ALLOC_N(def->ifs, MAX(nForwardIfs, 1)) < 0)
-            goto cleanup;
+            return -1;
 
         if (forwardDev) {
             def->ifs[0].device.dev = forwardDev;
@@ -1607,64 +1564,66 @@ virNetworkForwardDefParseXML(const char *networkName,
 
         /* parse each <interface> */
         for (i = 0; i < nForwardIfs; i++) {
-            forwardDev = virXMLPropString(forwardIfNodes[i], "dev");
-            if (!forwardDev) {
+            g_autofree char *forwardDevi = virXMLPropString(forwardIfNodes[i], "dev");
+
+            if (!forwardDevi) {
                 virReportError(VIR_ERR_XML_ERROR,
                                _("Missing required dev attribute in "
                                  "<forward> <interface> element of network %s"),
                                networkName);
-                goto cleanup;
+                return -1;
             }
 
             if ((i == 0) && (def->nifs == 1)) {
                 /* both <forward dev='x'> and <interface dev='x'/> are
                  * present.  If they don't match, it's an error.
                  */
-                if (STRNEQ(forwardDev, def->ifs[0].device.dev)) {
+                if (STRNEQ(forwardDevi, def->ifs[0].device.dev)) {
                     virReportError(VIR_ERR_XML_ERROR,
                                    _("<forward dev='%s'> must match first "
                                      "<interface dev='%s'/> in network %s"),
                                    def->ifs[0].device.dev,
-                                   forwardDev, networkName);
-                    goto cleanup;
+                                   forwardDevi, networkName);
+                    return -1;
                 }
-                VIR_FREE(forwardDev);
                 continue;
             }
 
             for (j = 0; j < i; j++) {
-                if (STREQ_NULLABLE(def->ifs[j].device.dev, forwardDev)) {
+                if (STREQ_NULLABLE(def->ifs[j].device.dev, forwardDevi)) {
                     virReportError(VIR_ERR_XML_ERROR,
                                    _("interface '%s' can only be "
                                      "listed once in network %s"),
-                                   forwardDev, networkName);
-                    goto cleanup;
+                                   forwardDevi, networkName);
+                    return -1;
                 }
             }
 
-            def->ifs[i].device.dev = forwardDev;
-            forwardDev = NULL;
+            def->ifs[i].device.dev = forwardDevi;
+            forwardDevi = NULL;
             def->ifs[i].type = VIR_NETWORK_FORWARD_HOSTDEV_DEVICE_NETDEV;
             def->nifs++;
         }
 
     } else if (nForwardAddrs > 0) {
         if (VIR_ALLOC_N(def->ifs, nForwardAddrs) < 0)
-            goto cleanup;
+            return -1;
 
         for (i = 0; i < nForwardAddrs; i++) {
-            if (!(type = virXMLPropString(forwardAddrNodes[i], "type"))) {
+            g_autofree char *addrType = NULL;
+
+            if (!(addrType = virXMLPropString(forwardAddrNodes[i], "type"))) {
                 virReportError(VIR_ERR_XML_ERROR,
                                _("missing address type in network %s"),
                                networkName);
-                goto cleanup;
+                return -1;
             }
 
-            if ((def->ifs[i].type = virNetworkForwardHostdevDeviceTypeFromString(type)) < 0) {
+            if ((def->ifs[i].type = virNetworkForwardHostdevDeviceTypeFromString(addrType)) < 0) {
                 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                                _("unknown address type '%s' in network %s"),
-                               type, networkName);
-                goto cleanup;
+                               addrType, networkName);
+                return -1;
             }
 
             switch (def->ifs[i].type) {
@@ -1673,7 +1632,7 @@ virNetworkForwardDefParseXML(const char *networkName,
                 virPCIDeviceAddressPtr addr = &def->ifs[i].device.pci;
 
                 if (virPCIDeviceAddressParseXML(forwardAddrNodes[i], addr) < 0)
-                    goto cleanup;
+                    return -1;
 
                 for (j = 0; j < i; j++) {
                     if (virPCIDeviceAddressEqual(addr, &def->ifs[j].device.pci)) {
@@ -1683,7 +1642,7 @@ virNetworkForwardDefParseXML(const char *networkName,
                                        addr->domain, addr->bus,
                                        addr->slot, addr->function,
                                        networkName);
-                        goto cleanup;
+                        return -1;
                     }
                 }
                 break;
@@ -1693,10 +1652,9 @@ virNetworkForwardDefParseXML(const char *networkName,
             default:
                 virReportError(VIR_ERR_XML_ERROR,
                                _("unsupported address type '%s' in network %s"),
-                               type, networkName);
-                goto cleanup;
+                               addrType, networkName);
+                return -1;
             }
-            VIR_FREE(type);
             def->nifs++;
         }
 
@@ -1704,10 +1662,10 @@ virNetworkForwardDefParseXML(const char *networkName,
         virReportError(VIR_ERR_XML_ERROR,
                        _("Only one <pf> element is allowed in <forward> of network %s"),
                        networkName);
-        goto cleanup;
+        return -1;
     } else if (nForwardPfs == 1) {
         if (VIR_ALLOC_N(def->pfs, nForwardPfs) < 0)
-            goto cleanup;
+            return -1;
 
         forwardDev = virXMLPropString(*forwardPfNodes, "dev");
         if (!forwardDev) {
@@ -1715,7 +1673,7 @@ virNetworkForwardDefParseXML(const char *networkName,
                            _("Missing required dev attribute "
                              "in <pf> element of network '%s'"),
                            networkName);
-            goto cleanup;
+            return -1;
         }
 
         def->pfs->dev = forwardDev;
@@ -1723,17 +1681,7 @@ virNetworkForwardDefParseXML(const char *networkName,
         def->npfs++;
     }
 
-    ret = 0;
- cleanup:
-    VIR_FREE(type);
-    VIR_FREE(forwardDev);
-    VIR_FREE(forwardManaged);
-    VIR_FREE(forwardDriverName);
-    VIR_FREE(forwardPfNodes);
-    VIR_FREE(forwardIfNodes);
-    VIR_FREE(forwardAddrNodes);
-    VIR_FREE(forwardNatNodes);
-    return ret;
+    return 0;
 }
 
 
@@ -1741,18 +1689,23 @@ virNetworkDefPtr
 virNetworkDefParseXML(xmlXPathContextPtr ctxt,
                       virNetworkXMLOptionPtr xmlopt)
 {
-    virNetworkDefPtr def;
-    char *tmp = NULL;
-    char *stp = NULL;
-    xmlNodePtr *ipNodes = NULL;
-    xmlNodePtr *routeNodes = NULL;
-    xmlNodePtr *portGroupNodes = NULL;
+    g_autoptr(virNetworkDef) def = NULL;
+    g_autofree char *uuid = NULL;
+    g_autofree char *localOnly = NULL;
+    g_autofree char *stp = NULL;
+    g_autofree char *stpDelay = NULL;
+    g_autofree char *macTableManager = NULL;
+    g_autofree char *macAddr = NULL;
+    g_autofree char *mtuSize = NULL;
+    g_autofree xmlNodePtr *ipNodes = NULL;
+    g_autofree xmlNodePtr *routeNodes = NULL;
+    g_autofree xmlNodePtr *portGroupNodes = NULL;
     int nips, nPortGroups, nRoutes;
     xmlNodePtr dnsNode = NULL;
     xmlNodePtr virtPortNode = NULL;
     xmlNodePtr forwardNode = NULL;
-    char *ipv6nogwStr = NULL;
-    char *trustGuestRxFilters = NULL;
+    g_autofree char *ipv6nogwStr = NULL;
+    g_autofree char *trustGuestRxFilters = NULL;
     VIR_XPATH_NODE_AUTORESTORE(ctxt);
     xmlNodePtr bandwidthNode = NULL;
     xmlNodePtr vlanNode;
@@ -1765,27 +1718,26 @@ virNetworkDefParseXML(xmlXPathContextPtr ctxt,
     def->name = virXPathString("string(./name[1])", ctxt);
     if (!def->name) {
         virReportError(VIR_ERR_NO_NAME, NULL);
-        goto error;
+        return NULL;
     }
 
     if (virXMLCheckIllegalChars("name", def->name, "/") < 0)
-        goto error;
+        return NULL;
 
     /* Extract network uuid */
-    tmp = virXPathString("string(./uuid[1])", ctxt);
-    if (!tmp) {
+    uuid = virXPathString("string(./uuid[1])", ctxt);
+    if (!uuid) {
         if (virUUIDGenerate(def->uuid) < 0) {
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            "%s", _("Failed to generate UUID"));
-            goto error;
+            return NULL;
         }
     } else {
-        if (virUUIDParse(tmp, def->uuid) < 0) {
+        if (virUUIDParse(uuid, def->uuid) < 0) {
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            "%s", _("malformed uuid element"));
-            goto error;
+            return NULL;
         }
-        VIR_FREE(tmp);
         def->uuid_specified = true;
     }
 
@@ -1798,9 +1750,8 @@ virNetworkDefParseXML(xmlXPathContextPtr ctxt,
             virReportError(VIR_ERR_XML_ERROR,
                            _("Invalid ipv6 setting '%s' in network '%s'"),
                            ipv6nogwStr, def->name);
-            goto error;
+            return NULL;
         }
-        VIR_FREE(ipv6nogwStr);
     }
 
     trustGuestRxFilters
@@ -1812,36 +1763,34 @@ virNetworkDefParseXML(xmlXPathContextPtr ctxt,
                            _("Invalid trustGuestRxFilters setting '%s' "
                              "in network '%s'"),
                            trustGuestRxFilters, def->name);
-            goto error;
+            return NULL;
         }
-        VIR_FREE(trustGuestRxFilters);
     }
 
     /* Parse network domain information */
     def->domain = virXPathString("string(./domain[1]/@name)", ctxt);
-    tmp = virXPathString("string(./domain[1]/@localOnly)", ctxt);
-    if (tmp) {
-        def->domainLocalOnly = virTristateBoolTypeFromString(tmp);
+    localOnly = virXPathString("string(./domain[1]/@localOnly)", ctxt);
+    if (localOnly) {
+        def->domainLocalOnly = virTristateBoolTypeFromString(localOnly);
         if (def->domainLocalOnly <= 0) {
             virReportError(VIR_ERR_XML_ERROR,
                            _("Invalid domain localOnly setting '%s' "
                              "in network '%s'"),
-                           tmp, def->name);
-            goto error;
+                           localOnly, def->name);
+            return NULL;
         }
-        VIR_FREE(tmp);
     }
 
     if ((bandwidthNode = virXPathNode("./bandwidth", ctxt)) &&
         virNetDevBandwidthParse(&def->bandwidth, NULL, bandwidthNode, false) < 0)
-        goto error;
+        return NULL;
 
     vlanNode = virXPathNode("./vlan", ctxt);
     if (vlanNode && virNetDevVlanParse(vlanNode, ctxt, &def->vlan) < 0)
-        goto error;
+        return NULL;
 
     if (virNetworkPortOptionsParseXML(ctxt, &def->isolatedPort) < 0)
-        goto error;
+        return NULL;
 
     /* Parse bridge information */
     def->bridge = virXPathString("string(./bridge[1]/@name)", ctxt);
@@ -1849,124 +1798,118 @@ virNetworkDefParseXML(xmlXPathContextPtr ctxt,
     stp = virXPathString("string(./bridge[1]/@stp)", ctxt);
     def->stp = (stp && STREQ(stp, "off")) ? false : true;
 
-    tmp = virXPathString("string(./bridge[1]/@delay)", ctxt);
-    if (tmp) {
-        if (virStrToLong_ulp(tmp, NULL, 10, &def->delay) < 0) {
+    stpDelay = virXPathString("string(./bridge[1]/@delay)", ctxt);
+    if (stpDelay) {
+        if (virStrToLong_ulp(stpDelay, NULL, 10, &def->delay) < 0) {
             virReportError(VIR_ERR_XML_ERROR,
                            _("Invalid delay value in network '%s'"),
                            def->name);
-            goto error;
+            return NULL;
         }
     }
-    VIR_FREE(tmp);
 
-    tmp = virXPathString("string(./bridge[1]/@macTableManager)", ctxt);
-    if (tmp) {
+    macTableManager = virXPathString("string(./bridge[1]/@macTableManager)", ctxt);
+    if (macTableManager) {
         if ((def->macTableManager
-             = virNetworkBridgeMACTableManagerTypeFromString(tmp)) <= 0) {
+             = virNetworkBridgeMACTableManagerTypeFromString(macTableManager)) <= 0) {
             virReportError(VIR_ERR_XML_ERROR,
                            _("Invalid macTableManager setting '%s' "
-                             "in network '%s'"), tmp, def->name);
-            goto error;
+                             "in network '%s'"), macTableManager, def->name);
+            return NULL;
         }
-        VIR_FREE(tmp);
     }
 
-    tmp = virXPathString("string(./mac[1]/@address)", ctxt);
-    if (tmp) {
-        if (virMacAddrParse(tmp, &def->mac) < 0) {
+    macAddr = virXPathString("string(./mac[1]/@address)", ctxt);
+    if (macAddr) {
+        if (virMacAddrParse(macAddr, &def->mac) < 0) {
             virReportError(VIR_ERR_XML_ERROR,
                            _("Invalid bridge mac address '%s' in network '%s'"),
-                           tmp, def->name);
-            goto error;
+                           macAddr, def->name);
+            return NULL;
         }
         if (virMacAddrIsMulticast(&def->mac)) {
             virReportError(VIR_ERR_XML_ERROR,
                            _("Invalid multicast bridge mac address '%s' in network '%s'"),
-                           tmp, def->name);
-            goto error;
+                           macAddr, def->name);
+            return NULL;
         }
-        VIR_FREE(tmp);
         def->mac_specified = true;
     }
 
-    tmp = virXPathString("string(./mtu/@size)", ctxt);
-    if (tmp) {
-        if (virStrToLong_ui(tmp, NULL, 10, &def->mtu) < 0) {
+    mtuSize = virXPathString("string(./mtu/@size)", ctxt);
+    if (mtuSize) {
+        if (virStrToLong_ui(mtuSize, NULL, 10, &def->mtu) < 0) {
             virReportError(VIR_ERR_XML_ERROR,
                            _("Invalid mtu size '%s' in network '%s'"),
-                           tmp, def->name);
-            goto error;
+                           mtuSize, def->name);
+            return NULL;
         }
     }
-    VIR_FREE(tmp);
 
     dnsNode = virXPathNode("./dns", ctxt);
     if (dnsNode != NULL &&
         virNetworkDNSDefParseXML(def->name, dnsNode, ctxt, &def->dns) < 0) {
-        goto error;
+        return NULL;
     }
 
     virtPortNode = virXPathNode("./virtualport", ctxt);
     if (virtPortNode &&
         (!(def->virtPortProfile = virNetDevVPortProfileParse(virtPortNode,
                                                              VIR_VPORT_XML_REQUIRE_TYPE)))) {
-        goto error;
+        return NULL;
     }
 
     nPortGroups = virXPathNodeSet("./portgroup", ctxt, &portGroupNodes);
     if (nPortGroups < 0)
-        goto error;
+        return NULL;
 
     if (nPortGroups > 0) {
         size_t i;
 
         /* allocate array to hold all the portgroups */
         if (VIR_ALLOC_N(def->portGroups, nPortGroups) < 0)
-            goto error;
+            return NULL;
         /* parse each portgroup */
         for (i = 0; i < nPortGroups; i++) {
             if (virNetworkPortGroupParseXML(&def->portGroups[i],
                                             portGroupNodes[i],
                                             ctxt) < 0)
-                goto error;
+                return NULL;
             def->nPortGroups++;
         }
     }
-    VIR_FREE(portGroupNodes);
 
     nips = virXPathNodeSet("./ip", ctxt, &ipNodes);
     if (nips < 0)
-        goto error;
+        return NULL;
 
     if (nips > 0) {
         size_t i;
 
         /* allocate array to hold all the addrs */
         if (VIR_ALLOC_N(def->ips, nips) < 0)
-            goto error;
+            return NULL;
         /* parse each addr */
         for (i = 0; i < nips; i++) {
             if (virNetworkIPDefParseXML(def->name,
                                         ipNodes[i],
                                         ctxt,
                                         &def->ips[i]) < 0)
-                goto error;
+                return NULL;
             def->nips++;
         }
     }
-    VIR_FREE(ipNodes);
 
     nRoutes = virXPathNodeSet("./route", ctxt, &routeNodes);
     if (nRoutes < 0)
-        goto error;
+        return NULL;
 
     if (nRoutes > 0) {
         size_t i;
 
         /* allocate array to hold all the route definitions */
         if (VIR_ALLOC_N(def->routes, nRoutes) < 0)
-            goto error;
+            return NULL;
         /* parse each definition */
         for (i = 0; i < nRoutes; i++) {
             virNetDevIPRoutePtr route = NULL;
@@ -1974,7 +1917,7 @@ virNetworkDefParseXML(xmlXPathContextPtr ctxt,
             if (!(route = virNetDevIPRouteParseXML(def->name,
                                                    routeNodes[i],
                                                    ctxt)))
-                goto error;
+                return NULL;
             def->routes[i] = route;
             def->nroutes++;
         }
@@ -2011,21 +1954,19 @@ virNetworkDefParseXML(xmlXPathContextPtr ctxt,
                 }
             }
             if (!addrMatch) {
-                char *gw = virSocketAddrFormat(gateway);
+                g_autofree char *gw = virSocketAddrFormat(gateway);
                 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                                _("unreachable static route gateway '%s' specified for network '%s'"),
                                gw, def->name);
-                VIR_FREE(gw);
-                goto error;
+                return NULL;
             }
         }
     }
-    VIR_FREE(routeNodes);
 
     forwardNode = virXPathNode("./forward", ctxt);
     if (forwardNode &&
         virNetworkForwardDefParseXML(def->name, forwardNode, ctxt, &def->forward) < 0) {
-        goto error;
+        return NULL;
     }
 
     /* Validate some items in the main NetworkDef that need to align
@@ -2047,14 +1988,14 @@ virNetworkDefParseXML(xmlXPathContextPtr ctxt,
                              "but no IP address provided for network '%s'"),
                            virNetworkForwardTypeToString(def->forward.type),
                            def->name);
-            goto error;
+            return NULL;
         }
         if (def->forward.nifs > 1) {
             virReportError(VIR_ERR_XML_ERROR,
                            _("multiple forwarding interfaces specified "
                              "for network '%s', only one is supported"),
                            def->name);
-            goto error;
+            return NULL;
         }
 
         if (def->forward.type == VIR_NETWORK_FORWARD_OPEN && def->forward.nifs) {
@@ -2067,7 +2008,7 @@ virNetworkDefParseXML(xmlXPathContextPtr ctxt,
                              "network '%s' with forward mode='%s'"),
                            def->name,
                            virNetworkForwardTypeToString(def->forward.type));
-            goto error;
+            return NULL;
         }
         break;
 
@@ -2080,14 +2021,14 @@ virNetworkDefParseXML(xmlXPathContextPtr ctxt,
                            _("bridge name not allowed in %s mode (network '%s')"),
                            virNetworkForwardTypeToString(def->forward.type),
                            def->name);
-            goto error;
+            return NULL;
         }
         if (def->bridgeZone) {
             virReportError(VIR_ERR_XML_ERROR,
                            _("bridge zone not allowed in %s mode (network '%s')"),
                            virNetworkForwardTypeToString(def->forward.type),
                            def->name);
-            goto error;
+            return NULL;
         }
         if (def->macTableManager) {
             virReportError(VIR_ERR_XML_ERROR,
@@ -2095,7 +2036,7 @@ virNetworkDefParseXML(xmlXPathContextPtr ctxt,
                              "in %s mode (network '%s')"),
                            virNetworkForwardTypeToString(def->forward.type),
                            def->name);
-            goto error;
+            return NULL;
         }
         G_GNUC_FALLTHROUGH;
 
@@ -2107,7 +2048,7 @@ virNetworkDefParseXML(xmlXPathContextPtr ctxt,
                              "(network '%s')"),
                            virNetworkForwardTypeToString(def->forward.type),
                            def->name);
-            goto error;
+            return NULL;
         }
         if (def->bridge && (def->forward.nifs || def->forward.npfs)) {
             virReportError(VIR_ERR_XML_ERROR,
@@ -2116,18 +2057,16 @@ virNetworkDefParseXML(xmlXPathContextPtr ctxt,
                              "both (network '%s')"),
                            virNetworkForwardTypeToString(def->forward.type),
                            def->name);
-            goto error;
+            return NULL;
         }
         break;
 
     case VIR_NETWORK_FORWARD_LAST:
     default:
         virReportEnumRangeError(virNetworkForwardType, def->forward.type);
-        goto error;
+        return NULL;
     }
 
-    VIR_FREE(stp);
-
     if (def->mtu) {
         switch ((virNetworkForwardType) def->forward.type) {
         case VIR_NETWORK_FORWARD_NONE:
@@ -2146,12 +2085,12 @@ virNetworkDefParseXML(xmlXPathContextPtr ctxt,
                              "and isolated mode, not in %s (network '%s')"),
                            virNetworkForwardTypeToString(def->forward.type),
                            def->name);
-            goto error;
+            return NULL;
 
         case VIR_NETWORK_FORWARD_LAST:
         default:
             virReportEnumRangeError(virNetworkForwardType, def->forward.type);
-            goto error;
+            return NULL;
         }
     }
 
@@ -2165,23 +2104,12 @@ virNetworkDefParseXML(xmlXPathContextPtr ctxt,
         def->ns = xmlopt->ns;
     if (def->ns.parse) {
         if (virXMLNamespaceRegister(ctxt, &def->ns) < 0)
-            goto error;
+            return NULL;
         if ((def->ns.parse)(ctxt, &def->namespaceData) < 0)
-            goto error;
+            return NULL;
     }
 
-    return def;
-
- error:
-    VIR_FREE(tmp);
-    VIR_FREE(routeNodes);
-    VIR_FREE(stp);
-    virNetworkDefFree(def);
-    VIR_FREE(ipNodes);
-    VIR_FREE(portGroupNodes);
-    VIR_FREE(ipv6nogwStr);
-    VIR_FREE(trustGuestRxFilters);
-    return NULL;
+    return g_steal_pointer(&def);
 }
 
 
@@ -2190,14 +2118,12 @@ virNetworkDefParse(const char *xmlStr,
                    const char *filename,
                    virNetworkXMLOptionPtr xmlopt)
 {
-    xmlDocPtr xml;
+    g_autoptr(xmlDoc) xml = NULL;
     virNetworkDefPtr def = NULL;
     int keepBlanksDefault = xmlKeepBlanksDefault(0);
 
-    if ((xml = virXMLParse(filename, xmlStr, _("(network_definition)")))) {
+    if ((xml = virXMLParse(filename, xmlStr, _("(network_definition)"))))
         def = virNetworkDefParseNode(xml, xmlDocGetRootElement(xml), xmlopt);
-        xmlFreeDoc(xml);
-    }
 
     xmlKeepBlanksDefault(keepBlanksDefault);
     return def;
@@ -2292,13 +2218,12 @@ virNetworkDNSDefFormat(virBufferPtr buf,
                                   def->forwarders[i].domain);
         }
         if (VIR_SOCKET_ADDR_VALID(&def->forwarders[i].addr)) {
-        char *addr = virSocketAddrFormat(&def->forwarders[i].addr);
+            g_autofree char *addr = virSocketAddrFormat(&def->forwarders[i].addr);
 
-        if (!addr)
-            return -1;
+            if (!addr)
+                return -1;
 
-        virBufferAsprintf(buf, " addr='%s'", addr);
-        VIR_FREE(addr);
+            virBufferAsprintf(buf, " addr='%s'", addr);
         }
         virBufferAddLit(buf, "/>\n");
     }
@@ -2331,7 +2256,7 @@ virNetworkDNSDefFormat(virBufferPtr buf,
 
     if (def->nhosts) {
         for (i = 0; i < def->nhosts; i++) {
-            char *ip = virSocketAddrFormat(&def->hosts[i].ip);
+            g_autofree char *ip = virSocketAddrFormat(&def->hosts[i].ip);
 
             virBufferAsprintf(buf, "<host ip='%s'>\n", ip);
             virBufferAdjustIndent(buf, 2);
@@ -2341,7 +2266,6 @@ virNetworkDNSDefFormat(virBufferPtr buf,
 
             virBufferAdjustIndent(buf, -2);
             virBufferAddLit(buf, "</host>\n");
-            VIR_FREE(ip);
         }
     }
     virBufferAdjustIndent(buf, -2);
@@ -2359,18 +2283,16 @@ virNetworkIPDefFormat(virBufferPtr buf,
     if (def->family)
         virBufferAsprintf(buf, " family='%s'", def->family);
     if (VIR_SOCKET_ADDR_VALID(&def->address)) {
-        char *addr = virSocketAddrFormat(&def->address);
+        g_autofree char *addr = virSocketAddrFormat(&def->address);
         if (!addr)
             return -1;
         virBufferAsprintf(buf, " address='%s'", addr);
-        VIR_FREE(addr);
     }
     if (VIR_SOCKET_ADDR_VALID(&def->netmask)) {
-        char *addr = virSocketAddrFormat(&def->netmask);
+        g_autofree char *addr = virSocketAddrFormat(&def->netmask);
         if (!addr)
             return -1;
         virBufferAsprintf(buf, " netmask='%s'", addr);
-        VIR_FREE(addr);
     }
     if (def->prefix > 0)
         virBufferAsprintf(buf, " prefix='%u'", def->prefix);
@@ -2395,15 +2317,15 @@ virNetworkIPDefFormat(virBufferPtr buf,
         for (i = 0; i < def->nranges; i++) {
             virSocketAddrRange addr = def->ranges[i].addr;
             virNetworkDHCPLeaseTimeDefPtr lease = def->ranges[i].lease;
+            g_autofree char *saddr = NULL;
+            g_autofree char *eaddr = NULL;
 
-            char *saddr = virSocketAddrFormat(&addr.start);
-            if (!saddr)
+            if (!(saddr = virSocketAddrFormat(&addr.start)))
                 return -1;
-            char *eaddr = virSocketAddrFormat(&addr.end);
-            if (!eaddr) {
-                VIR_FREE(saddr);
+
+            if (!(eaddr = virSocketAddrFormat(&addr.end)))
                 return -1;
-            }
+
             virBufferAsprintf(buf, "<range start='%s' end='%s'",
                               saddr, eaddr);
             if (lease) {
@@ -2421,8 +2343,6 @@ virNetworkIPDefFormat(virBufferPtr buf,
             } else {
                 virBufferAddLit(buf, "/>\n");
             }
-            VIR_FREE(saddr);
-            VIR_FREE(eaddr);
         }
         for (i = 0; i < def->nhosts; i++) {
             virNetworkDHCPLeaseTimeDefPtr lease = def->hosts[i].lease;
@@ -2434,11 +2354,11 @@ virNetworkIPDefFormat(virBufferPtr buf,
             if (def->hosts[i].name)
                 virBufferAsprintf(buf, " name='%s'", def->hosts[i].name);
             if (VIR_SOCKET_ADDR_VALID(&def->hosts[i].ip)) {
-                char *ipaddr = virSocketAddrFormat(&def->hosts[i].ip);
+                g_autofree char *ipaddr = virSocketAddrFormat(&def->hosts[i].ip);
                 if (!ipaddr)
                     return -1;
+
                 virBufferAsprintf(buf, " ip='%s'", ipaddr);
-                VIR_FREE(ipaddr);
             }
             if (lease) {
                 virBufferAddLit(buf, ">\n");
@@ -2460,11 +2380,11 @@ virNetworkIPDefFormat(virBufferPtr buf,
             virBufferEscapeString(buf, "<bootp file='%s'",
                                   def->bootfile);
             if (VIR_SOCKET_ADDR_VALID(&def->bootserver)) {
-                char *ipaddr = virSocketAddrFormat(&def->bootserver);
+                g_autofree char *ipaddr = virSocketAddrFormat(&def->bootserver);
                 if (!ipaddr)
                     return -1;
+
                 virBufferEscapeString(buf, " server='%s'", ipaddr);
-                VIR_FREE(ipaddr);
             }
             virBufferAddLit(buf, "/>\n");
 
@@ -2515,20 +2435,19 @@ static int
 virNetworkForwardNatDefFormat(virBufferPtr buf,
                               const virNetworkForwardDef *fwd)
 {
-    char *addrStart = NULL;
-    char *addrEnd = NULL;
-    int ret = -1;
+    g_autofree char *addrStart = NULL;
+    g_autofree char *addrEnd = NULL;
 
     if (VIR_SOCKET_ADDR_VALID(&fwd->addr.start)) {
         addrStart = virSocketAddrFormat(&fwd->addr.start);
         if (!addrStart)
-            goto cleanup;
+            return -1;
     }
 
     if (VIR_SOCKET_ADDR_VALID(&fwd->addr.end)) {
         addrEnd = virSocketAddrFormat(&fwd->addr.end);
         if (!addrEnd)
-            goto cleanup;
+            return -1;
     }
 
     if (!addrEnd && !addrStart && !fwd->port.start && !fwd->port.end && !fwd->natIPv6)
@@ -2561,12 +2480,7 @@ virNetworkForwardNatDefFormat(virBufferPtr buf,
 
     virBufferAdjustIndent(buf, -2);
     virBufferAddLit(buf, "</nat>\n");
-    ret = 0;
-
- cleanup:
-    VIR_FREE(addrStart);
-    VIR_FREE(addrEnd);
-    return ret;
+    return 0;
 }
 
 
@@ -2856,27 +2770,22 @@ virNetworkSaveXML(const char *configDir,
                   const char *xml)
 {
     char uuidstr[VIR_UUID_STRING_BUFLEN];
-    char *configFile = NULL;
-    int ret = -1;
+    g_autofree char *configFile = NULL;
 
     if ((configFile = virNetworkConfigFile(configDir, def->name)) == NULL)
-        goto cleanup;
+        return -1;
 
     if (virFileMakePath(configDir) < 0) {
         virReportSystemError(errno,
                              _("cannot create config directory '%s'"),
                              configDir);
-        goto cleanup;
+        return -1;
     }
 
     virUUIDFormat(def->uuid, uuidstr);
-    ret = virXMLSaveFile(configFile,
-                         virXMLPickShellSafeComment(def->name, uuidstr),
-                         "net-edit", xml);
-
- cleanup:
-    VIR_FREE(configFile);
-    return ret;
+    return virXMLSaveFile(configFile,
+                          virXMLPickShellSafeComment(def->name, uuidstr),
+                          "net-edit", xml);
 }
 
 
@@ -2885,19 +2794,15 @@ virNetworkSaveConfig(const char *configDir,
                      virNetworkDefPtr def,
                      virNetworkXMLOptionPtr xmlopt)
 {
-    int ret = -1;
-    char *xml;
+    g_autofree char *xml = NULL;
 
     if (!(xml = virNetworkDefFormat(def, xmlopt, VIR_NETWORK_XML_INACTIVE)))
-        goto cleanup;
+        return -1;
 
     if (virNetworkSaveXML(configDir, def, xml))
-        goto cleanup;
+        return -1;
 
-    ret = 0;
- cleanup:
-    VIR_FREE(xml);
-    return ret;
+    return 0;
 }
 
 
@@ -3113,14 +3018,13 @@ virNetworkDefUpdateIPDHCPHost(virNetworkDefPtr def,
         }
 
         if (i == ipdef->nhosts) {
-            char *ip = virSocketAddrFormat(&host.ip);
+            g_autofree char *ip = virSocketAddrFormat(&host.ip);
             virReportError(VIR_ERR_OPERATION_INVALID,
                            _("couldn't locate an existing dhcp host entry with "
                              "\"mac='%s'\" \"name='%s'\" \"ip='%s'\" in"
                              " network '%s'"),
                            host.mac ? host.mac : _("unknown"), host.name,
                            ip ? ip : _("unknown"), def->name);
-            VIR_FREE(ip);
             goto cleanup;
         }
 
@@ -3146,7 +3050,7 @@ virNetworkDefUpdateIPDHCPHost(virNetworkDefPtr def,
                  STREQ_NULLABLE(host.name, ipdef->hosts[i].name)) ||
                 (VIR_SOCKET_ADDR_VALID(&host.ip) &&
                  virSocketAddrEqual(&host.ip, &ipdef->hosts[i].ip))) {
-                char *ip = virSocketAddrFormat(&host.ip);
+                g_autofree char *ip = virSocketAddrFormat(&host.ip);
 
                 virReportError(VIR_ERR_OPERATION_INVALID,
                                _("there is an existing dhcp host entry in "
@@ -3154,7 +3058,6 @@ virNetworkDefUpdateIPDHCPHost(virNetworkDefPtr def,
                                  "\"<host mac='%s' name='%s' ip='%s'/>\""),
                                def->name, host.mac ? host.mac : _("unknown"),
                                host.name, ip ? ip : _("unknown"));
-                VIR_FREE(ip);
                 goto cleanup;
             }
         }
@@ -3258,8 +3161,8 @@ virNetworkDefUpdateIPDHCPRange(virNetworkDefPtr def,
             return -1;
 
         if (i < ipdef->nranges) {
-            char *startip = virSocketAddrFormat(&range.addr.start);
-            char *endip = virSocketAddrFormat(&range.addr.end);
+            g_autofree char *startip = virSocketAddrFormat(&range.addr.start);
+            g_autofree char *endip = virSocketAddrFormat(&range.addr.end);
 
             virReportError(VIR_ERR_OPERATION_INVALID,
                            _("there is an existing dhcp range entry in "
@@ -3268,8 +3171,6 @@ virNetworkDefUpdateIPDHCPRange(virNetworkDefPtr def,
                            def->name,
                            startip ? startip : "unknown",
                            endip ? endip : "unknown");
-            VIR_FREE(startip);
-            VIR_FREE(endip);
             return -1;
         }
 
diff --git a/src/conf/network_conf.h b/src/conf/network_conf.h
index e3a61c62ea..34b9aa9505 100644
--- a/src/conf/network_conf.h
+++ b/src/conf/network_conf.h
@@ -321,6 +321,7 @@ typedef enum {
 } virNetworkTaintFlags;
 
 void virNetworkDefFree(virNetworkDefPtr def);
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(virNetworkDef, virNetworkDefFree);
 
 enum {
     VIR_NETWORK_OBJ_LIST_ADD_LIVE = (1 << 0),
diff --git a/src/util/virxml.h b/src/util/virxml.h
index 26ab9f9c2d..ed178105f6 100644
--- a/src/util/virxml.h
+++ b/src/util/virxml.h
@@ -247,6 +247,7 @@ G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC(virXPathContextNodeSave, virXPathContextNodeRes
 
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(xmlDoc, xmlFreeDoc);
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(xmlXPathContext, xmlXPathFreeContext);
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(xmlBuffer, xmlBufferFree);
 
 typedef int (*virXMLNamespaceParse)(xmlXPathContextPtr ctxt, void **nsdata);
 typedef void (*virXMLNamespaceFree)(void *nsdata);
-- 
2.25.4





[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux