[PATCH 10/25] network: convert local pointers to g_auto*

Laine Stump laine at redhat.com
Thu Jun 25 03:33:59 UTC 2020


This includes those that use plain VIR_FREE() as well as those that
have a cleanup function defined for use via g_auto/g_autoptr
(virCommand, virFirewall, virBuffer, virJSONValue etc).

Signed-off-by: Laine Stump <laine at redhat.com>
---
 src/network/bridge_driver.c       | 477 +++++++++++-------------------
 src/network/bridge_driver_linux.c |  55 ++--
 src/network/leaseshelper.c        |  16 +-
 src/util/virdnsmasq.h             |   4 +
 4 files changed, 209 insertions(+), 343 deletions(-)

diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c
index aff1b7b1bc..668aa9ca88 100644
--- a/src/network/bridge_driver.c
+++ b/src/network/bridge_driver.c
@@ -322,25 +322,23 @@ networkRunHook(virNetworkObjPtr obj,
                int sub_op)
 {
     virNetworkDefPtr def;
-    virBuffer buf = VIR_BUFFER_INITIALIZER;
-    char *xml = NULL;
+    g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
+    g_autofree char *xml = NULL;
     int hookret;
-    int ret = -1;
 
     if (virHookPresent(VIR_HOOK_DRIVER_NETWORK)) {
         if (!obj) {
             VIR_DEBUG("Not running hook as @obj is NULL");
-            ret = 0;
-            goto cleanup;
+            return 0;
         }
         def = virNetworkObjGetDef(obj);
 
         virBufferAddLit(&buf, "<hookData>\n");
         virBufferAdjustIndent(&buf, 2);
         if (virNetworkDefFormatBuf(&buf, def, network_driver->xmlopt, 0) < 0)
-            goto cleanup;
+            return -1;
         if (port && virNetworkPortDefFormatBuf(&buf, port) < 0)
-            goto cleanup;
+            return -1;
 
         virBufferAdjustIndent(&buf, -2);
         virBufferAddLit(&buf, "</hookData>");
@@ -353,16 +351,12 @@ networkRunHook(virNetworkObjPtr obj,
          * If the script raised an error, pass it to the callee.
          */
         if (hookret < 0)
-            goto cleanup;
+            return -1;
 
         networkNetworkObjTaint(obj, VIR_NETWORK_TAINT_HOOK);
     }
 
-    ret = 0;
- cleanup:
-    virBufferFreeAndReset(&buf);
-    VIR_FREE(xml);
-    return ret;
+    return 0;
 }
 
 
@@ -426,44 +420,42 @@ static int
 networkRemoveInactive(virNetworkDriverStatePtr driver,
                       virNetworkObjPtr obj)
 {
-    char *leasefile = NULL;
-    char *customleasefile = NULL;
-    char *radvdconfigfile = NULL;
-    char *configfile = NULL;
-    char *radvdpidbase = NULL;
-    char *statusfile = NULL;
-    char *macMapFile = NULL;
-    dnsmasqContext *dctx = NULL;
+    g_autofree char *leasefile = NULL;
+    g_autofree char *customleasefile = NULL;
+    g_autofree char *radvdconfigfile = NULL;
+    g_autofree char *configfile = NULL;
+    g_autofree char *radvdpidbase = NULL;
+    g_autofree char *statusfile = NULL;
+    g_autofree char *macMapFile = NULL;
+    g_autoptr(dnsmasqContext) dctx = NULL;
     virNetworkDefPtr def = virNetworkObjGetPersistentDef(obj);
 
-    int ret = -1;
-
     /* remove the (possibly) existing dnsmasq and radvd files */
     if (!(dctx = dnsmasqContextNew(def->name,
                                    driver->dnsmasqStateDir))) {
-        goto cleanup;
+        return -1;
     }
 
     if (!(leasefile = networkDnsmasqLeaseFileNameDefault(driver, def->name)))
-        goto cleanup;
+        return -1;
 
     if (!(customleasefile = networkDnsmasqLeaseFileNameCustom(driver, def->bridge)))
-        goto cleanup;
+        return -1;
 
     if (!(radvdconfigfile = networkRadvdConfigFileName(driver, def->name)))
-        goto cleanup;
+        return -1;
 
     if (!(radvdpidbase = networkRadvdPidfileBasename(def->name)))
-        goto cleanup;
+        return -1;
 
     if (!(configfile = networkDnsmasqConfigFileName(driver, def->name)))
-        goto cleanup;
+        return -1;
 
     if (!(statusfile = virNetworkConfigFile(driver->stateDir, def->name)))
-        goto cleanup;
+        return -1;
 
     if (!(macMapFile = virMacMapFileName(driver->dnsmasqStateDir, def->bridge)))
-        goto cleanup;
+        return -1;
 
     /* dnsmasq */
     dnsmasqDelete(dctx);
@@ -484,18 +476,7 @@ networkRemoveInactive(virNetworkDriverStatePtr driver,
     /* remove the network definition */
     virNetworkObjRemoveInactive(driver->networks, obj);
 
-    ret = 0;
-
- cleanup:
-    VIR_FREE(leasefile);
-    VIR_FREE(configfile);
-    VIR_FREE(customleasefile);
-    VIR_FREE(radvdconfigfile);
-    VIR_FREE(radvdpidbase);
-    VIR_FREE(statusfile);
-    VIR_FREE(macMapFile);
-    dnsmasqContextFree(dctx);
-    return ret;
+    return 0;
 }
 
 
@@ -545,9 +526,9 @@ networkUpdateState(virNetworkObjPtr obj,
 {
     virNetworkDefPtr def;
     virNetworkDriverStatePtr driver = opaque;
-    dnsmasqCapsPtr dnsmasq_caps = networkGetDnsmasqCaps(driver);
+    g_autoptr(dnsmasqCaps) dnsmasq_caps = networkGetDnsmasqCaps(driver);
     virMacMapPtr macmap;
-    char *macMapFile = NULL;
+    g_autofree char *macMapFile = NULL;
     int ret = -1;
 
     virObjectLock(obj);
@@ -609,7 +590,7 @@ networkUpdateState(virNetworkObjPtr obj,
     if (virNetworkObjIsActive(obj) && def->ips && (def->nips > 0)) {
         pid_t radvdPid;
         pid_t dnsmasqPid;
-        char *radvdpidbase;
+        g_autofree char *radvdpidbase = NULL;
 
         ignore_value(virPidFileReadIfAlive(driver->pidDir,
                                            def->name,
@@ -625,14 +606,11 @@ networkUpdateState(virNetworkObjPtr obj,
                                            radvdpidbase,
                                            &radvdPid, RADVD));
         virNetworkObjSetRadvdPid(obj, radvdPid);
-        VIR_FREE(radvdpidbase);
     }
 
     ret = 0;
  cleanup:
     virObjectUnlock(obj);
-    virObjectUnref(dnsmasq_caps);
-    VIR_FREE(macMapFile);
     return ret;
 }
 
@@ -710,9 +688,8 @@ networkStateInitialize(bool privileged,
                        virStateInhibitCallback callback G_GNUC_UNUSED,
                        void *opaque G_GNUC_UNUSED)
 {
-    int ret = VIR_DRV_STATE_INIT_ERROR;
-    char *configdir = NULL;
-    char *rundir = NULL;
+    g_autofree char *configdir = NULL;
+    g_autofree char *rundir = NULL;
     bool autostart = true;
 #ifdef WITH_FIREWALLD
     DBusConnection *sysbus = NULL;
@@ -839,15 +816,12 @@ networkStateInitialize(bool privileged,
     }
 #endif
 
-    ret = VIR_DRV_STATE_INIT_COMPLETE;
- cleanup:
-    VIR_FREE(configdir);
-    VIR_FREE(rundir);
-    return ret;
+    return VIR_DRV_STATE_INIT_COMPLETE;
+
 
  error:
     networkStateCleanup();
-    goto cleanup;
+    return VIR_DRV_STATE_INIT_ERROR;
 }
 
 
@@ -1043,10 +1017,11 @@ networkDnsmasqConfLocalPTRs(virBufferPtr buf,
 {
     virNetworkIPDefPtr ip;
     size_t i;
-    char *ptr = NULL;
     int rc;
 
     for (i = 0; i < def->nips; i++) {
+        g_autofree char *ptr = NULL;
+
         ip = def->ips + i;
 
         if (ip->localPTR != VIR_TRISTATE_BOOL_YES)
@@ -1067,7 +1042,6 @@ networkDnsmasqConfLocalPTRs(virBufferPtr buf,
         }
 
         virBufferAsprintf(buf, "local=/%s/\n", ptr);
-        VIR_FREE(ptr);
     }
 
     return 0;
@@ -1083,15 +1057,14 @@ networkDnsmasqConfContents(virNetworkObjPtr obj,
                            dnsmasqCapsPtr caps G_GNUC_UNUSED)
 {
     virNetworkDefPtr def = virNetworkObjGetDef(obj);
-    virBuffer configbuf = VIR_BUFFER_INITIALIZER;
-    int r, ret = -1;
+    g_auto(virBuffer) configbuf = VIR_BUFFER_INITIALIZER;
+    int r;
     int nbleases = 0;
     size_t i;
     virNetworkDNSDefPtr dns = &def->dns;
     bool wantDNS = dns->enable != VIR_TRISTATE_BOOL_NO;
     virNetworkIPDefPtr tmpipdef, ipdef, ipv4def, ipv6def;
     bool ipv6SLAAC;
-    char *saddr = NULL, *eaddr = NULL;
 
     *configstr = NULL;
 
@@ -1146,12 +1119,11 @@ networkDnsmasqConfContents(virNetworkObjPtr obj,
             if (fwd->domain)
                 virBufferAsprintf(&configbuf, "/%s/", fwd->domain);
             if (VIR_SOCKET_ADDR_VALID(&fwd->addr)) {
-                char *addr = virSocketAddrFormat(&fwd->addr);
+                g_autofree char *addr = virSocketAddrFormat(&fwd->addr);
 
                 if (!addr)
-                    goto cleanup;
+                    return -1;
                 virBufferAsprintf(&configbuf, "%s\n", addr);
-                VIR_FREE(addr);
                 if (!fwd->domain)
                     addNoResolv = true;
             } else {
@@ -1177,7 +1149,7 @@ networkDnsmasqConfContents(virNetworkObjPtr obj,
 
     if (wantDNS &&
         networkDnsmasqConfLocalPTRs(&configbuf, def) < 0)
-        goto cleanup;
+        return -1;
 
     if (wantDNS && def->dns.forwardPlainNames == VIR_TRISTATE_BOOL_NO) {
         virBufferAddLit(&configbuf, "domain-needed\n");
@@ -1224,10 +1196,10 @@ networkDnsmasqConfContents(virNetworkObjPtr obj,
         for (i = 0;
              (tmpipdef = virNetworkDefGetIPByIndex(def, AF_UNSPEC, i));
              i++) {
-            char *ipaddr = virSocketAddrFormat(&tmpipdef->address);
+            g_autofree char *ipaddr = virSocketAddrFormat(&tmpipdef->address);
 
             if (!ipaddr)
-                goto cleanup;
+                return -1;
 
             /* also part of CVE 2012-3411 - if the host's version of
              * dnsmasq doesn't have bind-dynamic, only allow listening on
@@ -1250,11 +1222,9 @@ networkDnsmasqConfContents(virNetworkObjPtr obj,
                                  "(as described in RFC1918/RFC3484/RFC4193)."),
                                ipaddr, (int)version / 1000000,
                                (int)(version % 1000000) / 1000);
-                VIR_FREE(ipaddr);
-                goto cleanup;
+                return -1;
             }
             virBufferAsprintf(&configbuf, "listen-address=%s\n", ipaddr);
-            VIR_FREE(ipaddr);
         }
     }
 
@@ -1293,14 +1263,14 @@ networkDnsmasqConfContents(virNetworkObjPtr obj,
                                _("Missing required 'service' "
                                  "attribute in SRV record of network '%s'"),
                                def->name);
-                goto cleanup;
+                return -1;
             }
             if (!dns->srvs[i].protocol) {
                 virReportError(VIR_ERR_INTERNAL_ERROR,
                                _("Missing required 'service' "
                                  "attribute in SRV record of network '%s'"),
                                def->name);
-                goto cleanup;
+                return -1;
             }
             /* RFC2782 requires that service and protocol be preceded by
              * an underscore.
@@ -1349,7 +1319,7 @@ networkDnsmasqConfContents(virNetworkObjPtr obj,
                     virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                                    _("For IPv4, multiple DHCP definitions "
                                      "cannot be specified."));
-                    goto cleanup;
+                    return -1;
                 } else {
                     ipv4def = ipdef;
                 }
@@ -1369,13 +1339,13 @@ networkDnsmasqConfContents(virNetworkObjPtr obj,
                                    (int)(version % 1000000) / 1000,
                                    DNSMASQ_DHCPv6_MAJOR_REQD,
                                    DNSMASQ_DHCPv6_MINOR_REQD);
-                    goto cleanup;
+                    return -1;
                 }
                 if (ipv6def) {
                     virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                                    _("For IPv6, multiple DHCP definitions "
                                      "cannot be specified."));
-                    goto cleanup;
+                    return -1;
                 } else {
                     ipv6def = ipdef;
                 }
@@ -1404,16 +1374,18 @@ networkDnsmasqConfContents(virNetworkObjPtr obj,
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            _("bridge '%s' has an invalid prefix"),
                            def->bridge);
-            goto cleanup;
+            return -1;
         }
         for (r = 0; r < ipdef->nranges; r++) {
             int thisRange;
             virNetworkDHCPRangeDef range = ipdef->ranges[r];
             g_autofree char *leasetime = NULL;
+            g_autofree char *saddr = NULL;
+            g_autofree char *eaddr = NULL;
 
             if (!(saddr = virSocketAddrFormat(&range.addr.start)) ||
                 !(eaddr = virSocketAddrFormat(&range.addr.end)))
-                goto cleanup;
+                return -1;
 
             if (VIR_SOCKET_ADDR_IS_FAMILY(&ipdef->address, AF_INET6)) {
                virBufferAsprintf(&configbuf, "dhcp-range=%s,%s,%d",
@@ -1428,11 +1400,11 @@ networkDnsmasqConfContents(virNetworkObjPtr obj,
                                    _("Failed to translate bridge '%s' "
                                      "prefix %d to netmask"),
                                    def->bridge, prefix);
-                    goto cleanup;
+                    return -1;
                 }
 
                 if (!(netmaskStr = virSocketAddrFormat(&netmask)))
-                    goto cleanup;
+                    return -1;
                 virBufferAsprintf(&configbuf, "dhcp-range=%s,%s,%s",
                                   saddr, eaddr, netmaskStr);
             }
@@ -1442,14 +1414,12 @@ networkDnsmasqConfContents(virNetworkObjPtr obj,
 
             virBufferAddLit(&configbuf, "\n");
 
-            VIR_FREE(saddr);
-            VIR_FREE(eaddr);
             thisRange = virSocketAddrGetRange(&range.addr.start,
                                               &range.addr.end,
                                               &ipdef->address,
                                               virNetworkIPDefPrefix(ipdef));
             if (thisRange < 0)
-                goto cleanup;
+                return -1;
             nbleases += thisRange;
         }
 
@@ -1460,19 +1430,18 @@ networkDnsmasqConfContents(virNetworkObjPtr obj,
          * support)
          */
         if (!ipdef->nranges && ipdef->nhosts) {
-            char *bridgeaddr = virSocketAddrFormat(&ipdef->address);
+            g_autofree char *bridgeaddr = virSocketAddrFormat(&ipdef->address);
             if (!bridgeaddr)
-                goto cleanup;
+                return -1;
             virBufferAsprintf(&configbuf, "dhcp-range=%s,static",
                               bridgeaddr);
             if (VIR_SOCKET_ADDR_IS_FAMILY(&ipdef->address, AF_INET6))
                 virBufferAsprintf(&configbuf, ",%d", prefix);
             virBufferAddLit(&configbuf, "\n");
-            VIR_FREE(bridgeaddr);
         }
 
         if (networkBuildDnsmasqDhcpHostsList(dctx, ipdef) < 0)
-            goto cleanup;
+            return -1;
 
         /* Note: the following is IPv4 only */
         if (VIR_SOCKET_ADDR_IS_FAMILY(&ipdef->address, AF_INET)) {
@@ -1488,13 +1457,12 @@ networkDnsmasqConfContents(virNetworkObjPtr obj,
 
             if (ipdef->bootfile) {
                 if (VIR_SOCKET_ADDR_VALID(&ipdef->bootserver)) {
-                    char *bootserver = virSocketAddrFormat(&ipdef->bootserver);
+                    g_autofree char *bootserver = virSocketAddrFormat(&ipdef->bootserver);
 
                     if (!bootserver)
-                        goto cleanup;
+                        return -1;
                     virBufferAsprintf(&configbuf, "dhcp-boot=%s%s%s\n",
                                       ipdef->bootfile, ",,", bootserver);
-                    VIR_FREE(bootserver);
                 } else {
                     virBufferAsprintf(&configbuf, "dhcp-boot=%s\n", ipdef->bootfile);
                 }
@@ -1508,7 +1476,7 @@ networkDnsmasqConfContents(virNetworkObjPtr obj,
 
     /* this is done once per interface */
     if (networkBuildDnsmasqHostsList(dctx, dns) < 0)
-        goto cleanup;
+        return -1;
 
     /* Even if there are currently no static hosts, if we're
      * listening for DHCP, we should write a 0-length hosts
@@ -1539,12 +1507,11 @@ networkDnsmasqConfContents(virNetworkObjPtr obj,
                  (ipdef = virNetworkDefGetIPByIndex(def, AF_INET6, i));
                  i++) {
                 if (!(ipdef->nranges || ipdef->nhosts)) {
-                    char *bridgeaddr = virSocketAddrFormat(&ipdef->address);
+                    g_autofree char *bridgeaddr = virSocketAddrFormat(&ipdef->address);
                     if (!bridgeaddr)
-                        goto cleanup;
+                        return -1;
                     virBufferAsprintf(&configbuf,
                                       "dhcp-range=%s,ra-only\n", bridgeaddr);
-                    VIR_FREE(bridgeaddr);
                 }
             }
         }
@@ -1557,18 +1524,12 @@ networkDnsmasqConfContents(virNetworkObjPtr obj,
     }
 
     if (!(*configstr = virBufferContentAndReset(&configbuf)))
-        goto cleanup;
+        return -1;
 
     *hostsfilestr = dnsmasqDhcpHostsToString(dctx->hostsfile->hosts,
                                              dctx->hostsfile->nhosts);
 
-    ret = 0;
-
- cleanup:
-    VIR_FREE(saddr);
-    VIR_FREE(eaddr);
-    virBufferFreeAndReset(&configbuf);
-    return ret;
+    return 0;
 }
 
 
@@ -1581,39 +1542,38 @@ networkBuildDhcpDaemonCommandLine(virNetworkDriverStatePtr driver,
                                   dnsmasqContext *dctx)
 {
     virNetworkDefPtr def = virNetworkObjGetDef(obj);
-    dnsmasqCapsPtr dnsmasq_caps = networkGetDnsmasqCaps(driver);
-    virCommandPtr cmd = NULL;
-    int ret = -1;
-    char *configfile = NULL;
-    char *configstr = NULL;
-    char *hostsfilestr = NULL;
-    char *leaseshelper_path = NULL;
+    g_autoptr(dnsmasqCaps) dnsmasq_caps = networkGetDnsmasqCaps(driver);
+    g_autoptr(virCommand) cmd = NULL;
+    g_autofree char *configfile = NULL;
+    g_autofree char *configstr = NULL;
+    g_autofree char *hostsfilestr = NULL;
+    g_autofree char *leaseshelper_path = NULL;
 
     virNetworkObjSetDnsmasqPid(obj, -1);
 
     if (networkDnsmasqConfContents(obj, pidfile, &configstr, &hostsfilestr,
                                    dctx, dnsmasq_caps) < 0)
-        goto cleanup;
+        return -1;
     if (!configstr)
-        goto cleanup;
+        return -1;
 
     /* construct the filename */
     if (!(configfile = networkDnsmasqConfigFileName(driver, def->name)))
-        goto cleanup;
+        return -1;
 
     /* Write the file */
     if (virFileWriteStr(configfile, configstr, 0600) < 0) {
         virReportSystemError(errno,
                              _("couldn't write dnsmasq config file '%s'"),
                              configfile);
-        goto cleanup;
+        return -1;
     }
 
     /* This helper is used to create custom leases file for libvirt */
     if (!(leaseshelper_path = virFileFindResource("libvirt_leaseshelper",
                                                   abs_top_builddir "/src",
                                                   LIBEXECDIR)))
-        goto cleanup;
+        return -1;
 
     cmd = virCommandNew(dnsmasqCapsGetBinaryPath(dnsmasq_caps));
     virCommandAddArgFormat(cmd, "--conf-file=%s", configfile);
@@ -1622,15 +1582,8 @@ networkBuildDhcpDaemonCommandLine(virNetworkDriverStatePtr driver,
     virCommandAddArgFormat(cmd, "--dhcp-script=%s", leaseshelper_path);
     virCommandAddEnvPair(cmd, "VIR_BRIDGE_NAME", def->bridge);
 
-    *cmdout = cmd;
-    ret = 0;
- cleanup:
-    virObjectUnref(dnsmasq_caps);
-    VIR_FREE(configfile);
-    VIR_FREE(configstr);
-    VIR_FREE(hostsfilestr);
-    VIR_FREE(leaseshelper_path);
-    return ret;
+    *cmdout = g_steal_pointer(&cmd);
+    return 0;
 }
 
 
@@ -1642,11 +1595,10 @@ networkStartDhcpDaemon(virNetworkDriverStatePtr driver,
     virNetworkIPDefPtr ipdef;
     size_t i;
     bool needDnsmasq = false;
-    virCommandPtr cmd = NULL;
-    char *pidfile = NULL;
+    g_autoptr(virCommand) cmd = NULL;
+    g_autofree char *pidfile = NULL;
     pid_t dnsmasqPid;
-    int ret = -1;
-    dnsmasqContext *dctx = NULL;
+    g_autoptr(dnsmasqContext) dctx = NULL;
 
     /* see if there are any IP addresses that need a dhcp server */
     i = 0;
@@ -1656,53 +1608,46 @@ networkStartDhcpDaemon(virNetworkDriverStatePtr driver,
             needDnsmasq = true;
     }
 
-    if (i == 0) {
-        /* no IP addresses at all, so we don't need to run */
-        ret = 0;
-        goto cleanup;
-    }
+    /* no IP addresses at all, so we don't need to run */
+    if (i == 0)
+        return 0;
 
-    if (!needDnsmasq && def->dns.enable == VIR_TRISTATE_BOOL_NO) {
-        /* no DHCP services needed, and user disabled DNS service */
-        ret = 0;
-        goto cleanup;
-    }
+    /* no DHCP services needed, and user disabled DNS service */
+    if (!needDnsmasq && def->dns.enable == VIR_TRISTATE_BOOL_NO)
+        return 0;
 
     if (virFileMakePath(driver->pidDir) < 0) {
         virReportSystemError(errno,
                              _("cannot create directory %s"),
                              driver->pidDir);
-        goto cleanup;
+        return -1;
     }
 
     if (!(pidfile = virPidFileBuildPath(driver->pidDir, def->name)))
-        goto cleanup;
+        return -1;
 
     if (virFileMakePath(driver->dnsmasqStateDir) < 0) {
         virReportSystemError(errno,
                              _("cannot create directory %s"),
                              driver->dnsmasqStateDir);
-        goto cleanup;
+        return -1;
     }
 
     dctx = dnsmasqContextNew(def->name, driver->dnsmasqStateDir);
     if (dctx == NULL)
-        goto cleanup;
+        return -1;
 
     if (networkDnsmasqCapsRefresh(driver) < 0)
-        goto cleanup;
+        return -1;
 
-    ret = networkBuildDhcpDaemonCommandLine(driver, obj, &cmd, pidfile, dctx);
-    if (ret < 0)
-        goto cleanup;
+    if (networkBuildDhcpDaemonCommandLine(driver, obj, &cmd, pidfile, dctx) < 0)
+        return -1;
 
-    ret = dnsmasqSave(dctx);
-    if (ret < 0)
-        goto cleanup;
+    if (dnsmasqSave(dctx) < 0)
+        return -1;
 
-    ret = virCommandRun(cmd, NULL);
-    if (ret < 0)
-        goto cleanup;
+    if (virCommandRun(cmd, NULL) < 0)
+        return -1;
 
     /*
      * There really is no race here - when dnsmasq daemonizes, its
@@ -1712,17 +1657,12 @@ networkStartDhcpDaemon(virNetworkDriverStatePtr driver,
      * pid
      */
 
-    ret = virPidFileRead(driver->pidDir, def->name, &dnsmasqPid);
-    if (ret < 0)
-        goto cleanup;
+    if (virPidFileRead(driver->pidDir, def->name, &dnsmasqPid) < 0)
+        return -1;
+
     virNetworkObjSetDnsmasqPid(obj, dnsmasqPid);
 
-    ret = 0;
- cleanup:
-    VIR_FREE(pidfile);
-    virCommandFree(cmd);
-    dnsmasqContextFree(dctx);
-    return ret;
+    return 0;
 }
 
 
@@ -1738,11 +1678,10 @@ networkRefreshDhcpDaemon(virNetworkDriverStatePtr driver,
                          virNetworkObjPtr obj)
 {
     virNetworkDefPtr def = virNetworkObjGetDef(obj);
-    int ret = -1;
     size_t i;
     pid_t dnsmasqPid;
     virNetworkIPDefPtr ipdef, ipv4def, ipv6def;
-    dnsmasqContext *dctx = NULL;
+    g_autoptr(dnsmasqContext) dctx = NULL;
 
     /* if no IP addresses specified, nothing to do */
     if (!virNetworkDefGetIPByIndex(def, AF_UNSPEC, 0))
@@ -1754,10 +1693,8 @@ networkRefreshDhcpDaemon(virNetworkDriverStatePtr driver,
         return networkStartDhcpDaemon(driver, obj);
 
     VIR_INFO("Refreshing dnsmasq for network %s", def->bridge);
-    if (!(dctx = dnsmasqContextNew(def->name,
-                                   driver->dnsmasqStateDir))) {
-        goto cleanup;
-    }
+    if (!(dctx = dnsmasqContextNew(def->name, driver->dnsmasqStateDir)))
+        return -1;
 
     /* Look for first IPv4 address that has dhcp defined.
      * We only support dhcp-host config on one IPv4 subnetwork
@@ -1780,22 +1717,20 @@ networkRefreshDhcpDaemon(virNetworkDriverStatePtr driver,
     }
 
     if (ipv4def && (networkBuildDnsmasqDhcpHostsList(dctx, ipv4def) < 0))
-        goto cleanup;
+        return -1;
 
     if (ipv6def && (networkBuildDnsmasqDhcpHostsList(dctx, ipv6def) < 0))
-        goto cleanup;
+        return -1;
 
     if (networkBuildDnsmasqHostsList(dctx, &def->dns) < 0)
-        goto cleanup;
+        return -1;
 
-    if ((ret = dnsmasqSave(dctx)) < 0)
-        goto cleanup;
+    if (dnsmasqSave(dctx) < 0)
+        return -1;
 
     dnsmasqPid = virNetworkObjGetDnsmasqPid(obj);
-    ret = kill(dnsmasqPid, SIGHUP);
- cleanup:
-    dnsmasqContextFree(dctx);
-    return ret;
+    return kill(dnsmasqPid, SIGHUP);
+
 }
 
 
@@ -1833,8 +1768,7 @@ networkRadvdConfContents(virNetworkObjPtr obj,
                          char **configstr)
 {
     virNetworkDefPtr def = virNetworkObjGetDef(obj);
-    virBuffer configbuf = VIR_BUFFER_INITIALIZER;
-    int ret = -1;
+    g_auto(virBuffer) configbuf = VIR_BUFFER_INITIALIZER;
     size_t i;
     virNetworkIPDefPtr ipdef;
     bool v6present = false, dhcp6 = false;
@@ -1851,10 +1785,8 @@ networkRadvdConfContents(virNetworkObjPtr obj,
     }
 
     /* If there are no IPv6 addresses, then we are done */
-    if (!v6present) {
-        ret = 0;
-        goto cleanup;
-    }
+    if (!v6present)
+        return 0;
 
     /* create radvd config file appropriate for this network;
      * IgnoreIfMissing allows radvd to start even when the bridge is down
@@ -1872,33 +1804,29 @@ networkRadvdConfContents(virNetworkObjPtr obj,
     /* add a section for each IPv6 address in the config */
     for (i = 0; (ipdef = virNetworkDefGetIPByIndex(def, AF_INET6, i)); i++) {
         int prefix;
-        char *netaddr;
+        g_autofree char *netaddr = NULL;
 
         prefix = virNetworkIPDefPrefix(ipdef);
         if (prefix < 0) {
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            _("bridge '%s' has an invalid prefix"),
                            def->bridge);
-            goto cleanup;
+            return -1;
         }
         if (!(netaddr = virSocketAddrFormat(&ipdef->address)))
-            goto cleanup;
+            return -1;
+
         virBufferAsprintf(&configbuf,
                           "  prefix %s/%d\n"
                           "  {\n%s  };\n",
                           netaddr, prefix,
                           dhcp6 ? radvd2 : radvd3);
-        VIR_FREE(netaddr);
     }
 
     virBufferAddLit(&configbuf, "};\n");
-
     *configstr = virBufferContentAndReset(&configbuf);
+    return 0;
 
-    ret = 0;
- cleanup:
-    virBufferFreeAndReset(&configbuf);
-    return ret;
 }
 
 
@@ -1909,9 +1837,8 @@ networkRadvdConfWrite(virNetworkDriverStatePtr driver,
                       char **configFile)
 {
     virNetworkDefPtr def = virNetworkObjGetDef(obj);
-    int ret = -1;
-    char *configStr = NULL;
-    char *myConfigFile = NULL;
+    g_autofree char *configStr = NULL;
+    g_autofree char *myConfigFile = NULL;
 
     if (!configFile)
         configFile = &myConfigFile;
@@ -1919,29 +1846,24 @@ networkRadvdConfWrite(virNetworkDriverStatePtr driver,
     *configFile = NULL;
 
     if (networkRadvdConfContents(obj, &configStr) < 0)
-        goto cleanup;
+        return -1;
 
-    if (!configStr) {
-        ret = 0;
-        goto cleanup;
-    }
+    if (!configStr)
+        return 0;
 
     /* construct the filename */
     if (!(*configFile = networkRadvdConfigFileName(driver, def->name)))
-        goto cleanup;
+        return -1;
+
     /* write the file */
     if (virFileWriteStr(*configFile, configStr, 0600) < 0) {
         virReportSystemError(errno,
                              _("couldn't write radvd config file '%s'"),
                              *configFile);
-        goto cleanup;
+        return -1;
     }
 
-    ret = 0;
- cleanup:
-    VIR_FREE(configStr);
-    VIR_FREE(myConfigFile);
-    return ret;
+    return 0;
 }
 
 
@@ -1950,26 +1872,22 @@ networkStartRadvd(virNetworkDriverStatePtr driver,
                   virNetworkObjPtr obj)
 {
     virNetworkDefPtr def = virNetworkObjGetDef(obj);
-    dnsmasqCapsPtr dnsmasq_caps = networkGetDnsmasqCaps(driver);
+    g_autoptr(dnsmasqCaps) dnsmasq_caps = networkGetDnsmasqCaps(driver);
     pid_t radvdPid;
-    char *pidfile = NULL;
-    char *radvdpidbase = NULL;
-    char *configfile = NULL;
-    virCommandPtr cmd = NULL;
-    int ret = -1;
+    g_autofree char *pidfile = NULL;
+    g_autofree char *radvdpidbase = NULL;
+    g_autofree char *configfile = NULL;
+    g_autoptr(virCommand) cmd = NULL;
 
     virNetworkObjSetRadvdPid(obj, -1);
 
     /* Is dnsmasq handling RA? */
-    if (DNSMASQ_RA_SUPPORT(dnsmasq_caps)) {
-        ret = 0;
-        goto cleanup;
-    }
+    if (DNSMASQ_RA_SUPPORT(dnsmasq_caps))
+        return 0;
 
     if (!virNetworkDefGetIPByIndex(def, AF_INET6, 0)) {
         /* no IPv6 addresses, so we don't need to run radvd */
-        ret = 0;
-        goto cleanup;
+        return 0;
     }
 
     if (!virFileIsExecutable(RADVD)) {
@@ -1977,30 +1895,32 @@ networkStartRadvd(virNetworkDriverStatePtr driver,
                              _("Cannot find %s - "
                                "Possibly the package isn't installed"),
                              RADVD);
-        goto cleanup;
+        return -1;
     }
 
     if (virFileMakePath(driver->pidDir) < 0) {
         virReportSystemError(errno,
                              _("cannot create directory %s"),
                              driver->pidDir);
-        goto cleanup;
+        return -1;
     }
+
     if (virFileMakePath(driver->radvdStateDir) < 0) {
         virReportSystemError(errno,
                              _("cannot create directory %s"),
                              driver->radvdStateDir);
-        goto cleanup;
+        return -1;
     }
 
     /* construct pidfile name */
     if (!(radvdpidbase = networkRadvdPidfileBasename(def->name)))
-        goto cleanup;
+        return -1;
+
     if (!(pidfile = virPidFileBuildPath(driver->pidDir, radvdpidbase)))
-        goto cleanup;
+        return -1;
 
     if (networkRadvdConfWrite(driver, obj, &configfile) < 0)
-        goto cleanup;
+        return -1;
 
     /* prevent radvd from daemonizing itself with "--debug 1", and use
      * a dummy pidfile name - virCommand will create the pidfile we
@@ -2020,20 +1940,13 @@ networkStartRadvd(virNetworkDriverStatePtr driver,
     virCommandDaemonize(cmd);
 
     if (virCommandRun(cmd, NULL) < 0)
-        goto cleanup;
+        return -1;
 
     if (virPidFileRead(driver->pidDir, radvdpidbase, &radvdPid) < 0)
-        goto cleanup;
-    virNetworkObjSetRadvdPid(obj, radvdPid);
+        return -1;
 
-    ret = 0;
- cleanup:
-    virObjectUnref(dnsmasq_caps);
-    virCommandFree(cmd);
-    VIR_FREE(configfile);
-    VIR_FREE(radvdpidbase);
-    VIR_FREE(pidfile);
-    return ret;
+    virNetworkObjSetRadvdPid(obj, radvdPid);
+    return 0;
 }
 
 
@@ -2042,14 +1955,13 @@ networkRefreshRadvd(virNetworkDriverStatePtr driver,
                     virNetworkObjPtr obj)
 {
     virNetworkDefPtr def = virNetworkObjGetDef(obj);
-    dnsmasqCapsPtr dnsmasq_caps = networkGetDnsmasqCaps(driver);
+    g_autoptr(dnsmasqCaps) dnsmasq_caps = networkGetDnsmasqCaps(driver);
     g_autofree char *radvdpidbase = NULL;
     g_autofree char *pidfile = NULL;
     pid_t radvdPid;
 
     /* Is dnsmasq handling RA? */
     if (DNSMASQ_RA_SUPPORT(dnsmasq_caps)) {
-        virObjectUnref(dnsmasq_caps);
         if ((radvdpidbase = networkRadvdPidfileBasename(def->name)) &&
             (pidfile = virPidFileBuildPath(driver->pidDir, radvdpidbase))) {
             /* radvd should not be running but in case it is */
@@ -2058,7 +1970,6 @@ networkRefreshRadvd(virNetworkDriverStatePtr driver,
         }
         return 0;
     }
-    virObjectUnref(dnsmasq_caps);
 
     /* if there's no running radvd, just start it */
     radvdPid = virNetworkObjGetRadvdPid(obj);
@@ -2250,8 +2161,7 @@ static int
 networkSetIPv6Sysctls(virNetworkObjPtr obj)
 {
     virNetworkDefPtr def = virNetworkObjGetDef(obj);
-    char *field = NULL;
-    int ret = -1;
+    g_autofree char *field = NULL;
     bool enableIPv6 = !!virNetworkDefGetIPByIndex(def, AF_INET6, 0);
 
     /* set disable_ipv6 if there are no ipv6 addresses defined for the
@@ -2265,17 +2175,15 @@ networkSetIPv6Sysctls(virNetworkObjPtr obj)
         if (!enableIPv6)
             VIR_DEBUG("ipv6 appears to already be disabled on %s",
                       def->bridge);
-        ret = 0;
-        goto cleanup;
+        return 0;
     }
 
     if (virFileWriteStr(field, enableIPv6 ? "0" : "1", 0) < 0) {
         virReportSystemError(errno,
                              _("cannot write to %s to enable/disable IPv6 "
                                "on bridge %s"), field, def->bridge);
-        goto cleanup;
+        return -1;
     }
-    VIR_FREE(field);
 
     /* The rest of the ipv6 sysctl tunables should always be set the
      * same, whether or not we're using ipv6 on this bridge.
@@ -2284,31 +2192,29 @@ networkSetIPv6Sysctls(virNetworkObjPtr obj)
     /* Prevent guests from hijacking the host network by sending out
      * their own router advertisements.
      */
+    VIR_FREE(field);
     field = g_strdup_printf(SYSCTL_PATH "/net/ipv6/conf/%s/accept_ra",
                             def->bridge);
 
     if (virFileWriteStr(field, "0", 0) < 0) {
         virReportSystemError(errno,
                              _("cannot disable %s"), field);
-        goto cleanup;
+        return -1;
     }
-    VIR_FREE(field);
 
     /* All interfaces used as a gateway (which is what this is, by
      * definition), must always have autoconf=0.
      */
+    VIR_FREE(field);
     field = g_strdup_printf(SYSCTL_PATH "/net/ipv6/conf/%s/autoconf", def->bridge);
 
     if (virFileWriteStr(field, "0", 0) < 0) {
         virReportSystemError(errno,
                              _("cannot disable %s"), field);
-        goto cleanup;
+        return -1;
     }
 
-    ret = 0;
- cleanup:
-    VIR_FREE(field);
-    return ret;
+    return 0;
 }
 
 
@@ -2386,7 +2292,8 @@ networkWaitDadFinish(virNetworkObjPtr obj)
 {
     virNetworkDefPtr def = virNetworkObjGetDef(obj);
     virNetworkIPDefPtr ipdef;
-    virSocketAddrPtr *addrs = NULL, addr = NULL;
+    g_autofree virSocketAddrPtr *addrs = NULL;
+    virSocketAddrPtr addr = NULL;
     size_t naddrs = 0;
     int ret = -1;
 
@@ -2401,7 +2308,6 @@ networkWaitDadFinish(virNetworkObjPtr obj)
     ret = (naddrs == 0) ? 0 : virNetDevIPWaitDadFinish(addrs, naddrs);
 
  cleanup:
-    VIR_FREE(addrs);
     VIR_DEBUG("Finished waiting for IPv6 DAD on network %s with status %d",
               def->name, ret);
     return ret;
@@ -2418,9 +2324,9 @@ networkStartNetworkVirtual(virNetworkDriverStatePtr driver,
     virErrorPtr save_err = NULL;
     virNetworkIPDefPtr ipdef;
     virNetDevIPRoutePtr routedef;
-    char *macTapIfName = NULL;
+    g_autofree char *macTapIfName = NULL;
     virMacMapPtr macmap;
-    char *macMapFile = NULL;
+    g_autofree char *macMapFile = NULL;
     int tapfd = -1;
     bool dnsmasqStarted = false;
     bool devOnline = false;
@@ -2467,7 +2373,6 @@ networkStartNetworkVirtual(virNetworkDriverStatePtr driver,
                                            VIR_NETDEV_TAP_CREATE_USE_MAC_FOR_BRIDGE |
                                            VIR_NETDEV_TAP_CREATE_IFUP |
                                            VIR_NETDEV_TAP_CREATE_PERSIST) < 0) {
-            VIR_FREE(macTapIfName);
             goto error;
         }
     }
@@ -2583,9 +2488,6 @@ networkStartNetworkVirtual(virNetworkDriverStatePtr driver,
     if (virNetDevBandwidthSet(def->bridge, def->bandwidth, true, true) < 0)
         goto error;
 
-    VIR_FREE(macTapIfName);
-    VIR_FREE(macMapFile);
-
     return 0;
 
  error:
@@ -2609,10 +2511,8 @@ networkStartNetworkVirtual(virNetworkDriverStatePtr driver,
     if (macTapIfName) {
         VIR_FORCE_CLOSE(tapfd);
         ignore_value(virNetDevTapDelete(macTapIfName, NULL));
-        VIR_FREE(macTapIfName);
     }
     virNetworkObjUnrefMacMap(obj);
-    VIR_FREE(macMapFile);
 
     ignore_value(virNetDevBridgeDelete(def->bridge));
 
@@ -2637,14 +2537,12 @@ networkShutdownNetworkVirtual(virNetworkDriverStatePtr driver,
 
     radvdPid = virNetworkObjGetRadvdPid(obj);
     if (radvdPid > 0) {
-        char *radvdpidbase;
+        g_autofree char *radvdpidbase = NULL;
 
         kill(radvdPid, SIGTERM);
         /* attempt to delete the pidfile we created */
-        if ((radvdpidbase = networkRadvdPidfileBasename(def->name))) {
+        if ((radvdpidbase = networkRadvdPidfileBasename(def->name)))
             virPidFileDelete(driver->pidDir, radvdpidbase);
-            VIR_FREE(radvdpidbase);
-        }
     }
 
     dnsmasqPid = virNetworkObjGetDnsmasqPid(obj);
@@ -2652,11 +2550,9 @@ networkShutdownNetworkVirtual(virNetworkDriverStatePtr driver,
         kill(dnsmasqPid, SIGTERM);
 
     if (def->mac_specified) {
-        char *macTapIfName = networkBridgeDummyNicName(def->bridge);
-        if (macTapIfName) {
+        g_autofree char *macTapIfName = networkBridgeDummyNicName(def->bridge);
+        if (macTapIfName)
             ignore_value(virNetDevTapDelete(macTapIfName, NULL));
-            VIR_FREE(macTapIfName);
-        }
     }
 
     ignore_value(virNetDevSetOnline(def->bridge, 0));
@@ -2963,7 +2859,7 @@ networkShutdownNetwork(virNetworkDriverStatePtr driver,
 {
     virNetworkDefPtr def = virNetworkObjGetDef(obj);
     int ret = 0;
-    char *stateFile;
+    g_autofree char *stateFile = NULL;
 
     VIR_INFO("Shutting down network '%s'", def->name);
 
@@ -2975,7 +2871,6 @@ networkShutdownNetwork(virNetworkDriverStatePtr driver,
         return -1;
 
     unlink(stateFile);
-    VIR_FREE(stateFile);
 
     switch ((virNetworkForwardType) def->forward.type) {
 
@@ -3247,8 +3142,7 @@ static int
 networkFindUnusedBridgeName(virNetworkObjListPtr nets,
                             virNetworkDefPtr def)
 {
-    int ret = -1, id = 0;
-    char *newname = NULL;
+    int id = 0;
     const char *templ = "virbr%d";
     const char *p;
 
@@ -3258,7 +3152,8 @@ networkFindUnusedBridgeName(virNetworkObjListPtr nets,
         templ = def->bridge;
 
     do {
-        newname = g_strdup_printf(templ, id);
+        g_autofree char *newname = g_strdup_printf(templ, id);
+
         /* check if this name is used in another libvirt network or
          * there is an existing device with that name. ignore errors
          * from virNetDevExists(), just in case it isn't implemented
@@ -3267,21 +3162,15 @@ networkFindUnusedBridgeName(virNetworkObjListPtr nets,
         if (!(virNetworkObjBridgeInUse(nets, newname, def->name) ||
               virNetDevExists(newname) == 1)) {
             VIR_FREE(def->bridge); /*could contain template */
-            def->bridge = newname;
-            ret = 0;
-            goto cleanup;
+            def->bridge = g_steal_pointer(&newname);
+            return 0;
         }
-        VIR_FREE(newname);
     } while (++id <= MAX_BRIDGE_ID);
 
     virReportError(VIR_ERR_INTERNAL_ERROR,
                    _("Bridge generation exceeded max id %d"),
                    MAX_BRIDGE_ID);
-    ret = 0;
- cleanup:
-    if (ret < 0)
-        VIR_FREE(newname);
-    return ret;
+    return -1;
 }
 
 
@@ -3424,7 +3313,7 @@ networkValidate(virNetworkDriverStatePtr driver,
      */
     for (i = 0; i < def->forward.nifs; i++) {
         virNetworkForwardIfDefPtr iface = &def->forward.ifs[i];
-        char *sysfs_path = NULL;
+        g_autofree char *sysfs_path = NULL;
 
         switch ((virNetworkForwardHostdevDeviceType)iface->type) {
         case VIR_NETWORK_FORWARD_HOSTDEV_DEVICE_NETDEV:
@@ -3465,10 +3354,8 @@ networkValidate(virNetworkDriverStatePtr driver,
                                _("device '%s' in network '%s' is not "
                                  "an SR-IOV Virtual Function"),
                                sysfs_path, def->name);
-                VIR_FREE(sysfs_path);
                 return -1;
             }
-            VIR_FREE(sysfs_path);
             break;
         }
 
@@ -4144,7 +4031,8 @@ networkSetAutostart(virNetworkPtr net,
     virNetworkDriverStatePtr driver = networkGetDriver();
     virNetworkObjPtr obj;
     virNetworkDefPtr def;
-    char *configFile = NULL, *autostartLink = NULL;
+    g_autofree char *configFile = NULL;
+    g_autofree char *autostartLink = NULL;
     bool new_autostart;
     bool cur_autostart;
     int ret = -1;
@@ -4201,8 +4089,6 @@ networkSetAutostart(virNetworkPtr net,
     ret = 0;
 
  cleanup:
-    VIR_FREE(configFile);
-    VIR_FREE(autostartLink);
     virNetworkObjEndAPI(&obj);
     return ret;
 }
@@ -4224,12 +4110,12 @@ networkGetDHCPLeases(virNetworkPtr net,
     long long currtime = 0;
     long long expirytime_tmp = -1;
     bool ipv6 = false;
-    char *lease_entries = NULL;
-    char *custom_lease_file = NULL;
+    g_autofree char *lease_entries = NULL;
+    g_autofree char *custom_lease_file = NULL;
     const char *ip_tmp = NULL;
     const char *mac_tmp = NULL;
     virJSONValuePtr lease_tmp = NULL;
-    virJSONValuePtr leases_array = NULL;
+    g_autoptr(virJSONValue) leases_array = NULL;
     virNetworkIPDefPtr ipdef_tmp = NULL;
     virNetworkDHCPLeasePtr lease = NULL;
     virNetworkDHCPLeasePtr *leases_ret = NULL;
@@ -4384,10 +4270,6 @@ networkGetDHCPLeases(virNetworkPtr net,
 
  cleanup:
     VIR_FREE(lease);
-    VIR_FREE(lease_entries);
-    VIR_FREE(custom_lease_file);
-    virJSONValueFree(leases_array);
-
     virNetworkObjEndAPI(&obj);
 
     return rv;
@@ -5582,7 +5464,7 @@ networkPortSetParameters(virNetworkPortPtr port,
     virNetworkDefPtr def;
     virNetworkPortDefPtr portdef;
     virNetDevBandwidthPtr bandwidth = NULL;
-    char *dir = NULL;
+    g_autofree char *dir = NULL;
     int ret = -1;
     size_t i;
 
@@ -5660,7 +5542,6 @@ networkPortSetParameters(virNetworkPortPtr port,
  cleanup:
     virNetDevBandwidthFree(bandwidth);
     virNetworkObjEndAPI(&obj);
-    VIR_FREE(dir);
     return ret;
 }
 
diff --git a/src/network/bridge_driver_linux.c b/src/network/bridge_driver_linux.c
index 30f6aa8fe1..0d0ac730f2 100644
--- a/src/network/bridge_driver_linux.c
+++ b/src/network/bridge_driver_linux.c
@@ -217,14 +217,15 @@ void networkPostReloadFirewallRules(bool startup G_GNUC_UNUSED)
  */
 int networkCheckRouteCollision(virNetworkDefPtr def)
 {
-    int ret = 0, len;
-    char *cur, *buf = NULL;
+    int len;
+    char *cur;
+    g_autofree char *buf = NULL;
     /* allow for up to 100000 routes (each line is 128 bytes) */
     enum {MAX_ROUTE_SIZE = 128*100000};
 
     /* Read whole routing table into memory */
     if ((len = virFileReadAll(PROC_NET_ROUTE, MAX_ROUTE_SIZE, &buf)) < 0)
-        goto out;
+        return 0;
 
     /* Dropping the last character shouldn't hurt */
     if (len > 0)
@@ -233,7 +234,7 @@ int networkCheckRouteCollision(virNetworkDefPtr def)
     VIR_DEBUG("%s output:\n%s", PROC_NET_ROUTE, buf);
 
     if (!STRPREFIX(buf, "Iface"))
-        goto out;
+        return 0;
 
     /* First line is just headings, skip it */
     cur = strchr(buf, '\n');
@@ -295,8 +296,7 @@ int networkCheckRouteCollision(virNetworkDefPtr def)
                 virReportError(VIR_ERR_INTERNAL_ERROR,
                                _("Network is already in use by interface %s"),
                                iface);
-                ret = -1;
-                goto out;
+                return -1;
             }
         }
 
@@ -315,23 +315,19 @@ int networkCheckRouteCollision(virNetworkDefPtr def)
 
             if ((r_addr.data.inet4.sin_addr.s_addr == addr_val) &&
                 (r_mask.data.inet4.sin_addr.s_addr == mask_val)) {
-                char *addr_str = virSocketAddrFormat(&r_addr);
+                g_autofree char *addr_str = virSocketAddrFormat(&r_addr);
                 if (!addr_str)
                     virResetLastError();
                 virReportError(VIR_ERR_INTERNAL_ERROR,
                                _("Route address '%s' conflicts "
                                  "with IP address for '%s'"),
                                NULLSTR(addr_str), iface);
-                VIR_FREE(addr_str);
-                ret = -1;
-                goto out;
+                return -1;
             }
         }
     }
 
- out:
-    VIR_FREE(buf);
-    return ret;
+    return 0;
 }
 
 static const char networkLocalMulticastIPv4[] = "224.0.0.0/24";
@@ -838,8 +834,7 @@ int networkAddFirewallRules(virNetworkDefPtr def)
 {
     size_t i;
     virNetworkIPDefPtr ipdef;
-    virFirewallPtr fw = NULL;
-    int ret = -1;
+    g_autoptr(virFirewall) fw = NULL;
 
     if (virOnce(&createdOnce, networkSetupPrivateChains) < 0)
         return -1;
@@ -869,11 +864,11 @@ int networkAddFirewallRules(virNetworkDefPtr def)
                            _("zone %s requested for network %s "
                              "but firewalld is not active"),
                            def->bridgeZone, def->name);
-            goto cleanup;
+            return -1;
         }
 
         if (virFirewallDInterfaceSetZone(def->bridge, def->bridgeZone) < 0)
-            goto cleanup;
+            return -1;
 
     } else {
 
@@ -893,13 +888,13 @@ int networkAddFirewallRules(virNetworkDefPtr def)
              */
             if (virFirewallDZoneExists("libvirt")) {
                 if (virFirewallDInterfaceSetZone(def->bridge, "libvirt") < 0)
-                    goto cleanup;
+                    return -1;
             } else {
                 unsigned long version;
                 int vresult = virFirewallDGetVersion(&version);
 
                 if (vresult < 0)
-                    goto cleanup;
+                    return -1;
 
                 /* Support for nftables backend was added in firewalld
                  * 0.6.0. Support for rule priorities (required by the
@@ -919,7 +914,7 @@ int networkAddFirewallRules(virNetworkDefPtr def)
                                      "version supporting rule priorities "
                                      "(0.7.0+) and/or rebuilding "
                                      "libvirt with --with-firewalld-zone"));
-                    goto cleanup;
+                    return -1;
                 }
             }
         }
@@ -935,7 +930,7 @@ int networkAddFirewallRules(virNetworkDefPtr def)
          (ipdef = virNetworkDefGetIPByIndex(def, AF_UNSPEC, i));
          i++) {
         if (networkAddIPSpecificFirewallRules(fw, def, ipdef) < 0)
-            goto cleanup;
+            return -1;
     }
 
     virFirewallStartRollback(fw, 0);
@@ -944,7 +939,7 @@ int networkAddFirewallRules(virNetworkDefPtr def)
          (ipdef = virNetworkDefGetIPByIndex(def, AF_UNSPEC, i));
          i++) {
         if (networkRemoveIPSpecificFirewallRules(fw, def, ipdef) < 0)
-            goto cleanup;
+            return -1;
     }
     networkRemoveGeneralFirewallRules(fw, def);
 
@@ -952,12 +947,9 @@ int networkAddFirewallRules(virNetworkDefPtr def)
     networkAddChecksumFirewallRules(fw, def);
 
     if (virFirewallApply(fw) < 0)
-        goto cleanup;
+        return -1;
 
-    ret = 0;
- cleanup:
-    virFirewallFree(fw);
-    return ret;
+    return 0;
 }
 
 /* Remove all rules for all ip addresses (and general rules) on a network */
@@ -965,9 +957,7 @@ void networkRemoveFirewallRules(virNetworkDefPtr def)
 {
     size_t i;
     virNetworkIPDefPtr ipdef;
-    virFirewallPtr fw = NULL;
-
-    fw = virFirewallNew();
+    g_autoptr(virFirewall) fw = virFirewallNew();
 
     virFirewallStartTransaction(fw, VIR_FIREWALL_TRANSACTION_IGNORE_ERRORS);
     networkRemoveChecksumFirewallRules(fw, def);
@@ -978,12 +968,9 @@ void networkRemoveFirewallRules(virNetworkDefPtr def)
          (ipdef = virNetworkDefGetIPByIndex(def, AF_UNSPEC, i));
          i++) {
         if (networkRemoveIPSpecificFirewallRules(fw, def, ipdef) < 0)
-            goto cleanup;
+            return;
     }
     networkRemoveGeneralFirewallRules(fw, def);
 
     virFirewallApply(fw);
-
- cleanup:
-    virFirewallFree(fw);
 }
diff --git a/src/network/leaseshelper.c b/src/network/leaseshelper.c
index 2b5fc0f442..732dd09610 100644
--- a/src/network/leaseshelper.c
+++ b/src/network/leaseshelper.c
@@ -82,8 +82,8 @@ VIR_ENUM_IMPL(virLeaseAction,
 int
 main(int argc, char **argv)
 {
-    char *pid_file = NULL;
-    char *custom_lease_file = NULL;
+    g_autofree char *pid_file = NULL;
+    g_autofree char *custom_lease_file = NULL;
     const char *ip = NULL;
     const char *mac = NULL;
     const char *leases_str = NULL;
@@ -91,13 +91,13 @@ main(int argc, char **argv)
     const char *clientid = getenv("DNSMASQ_CLIENT_ID");
     const char *interface = getenv("DNSMASQ_INTERFACE");
     const char *hostname = getenv("DNSMASQ_SUPPLIED_HOSTNAME");
-    char *server_duid = NULL;
+    g_autofree char *server_duid = NULL;
     int action = -1;
     int pid_file_fd = -1;
     int rv = EXIT_FAILURE;
     bool delete = false;
-    virJSONValuePtr lease_new = NULL;
-    virJSONValuePtr leases_array_new = NULL;
+    g_autoptr(virJSONValue) lease_new = NULL;
+    g_autoptr(virJSONValue) leases_array_new = NULL;
 
     virSetErrorFunc(NULL, NULL);
     virSetErrorLogPriorityFunc(NULL);
@@ -256,11 +256,5 @@ main(int argc, char **argv)
     if (pid_file_fd != -1)
         virPidFileReleasePath(pid_file, pid_file_fd);
 
-    VIR_FREE(pid_file);
-    VIR_FREE(server_duid);
-    VIR_FREE(custom_lease_file);
-    virJSONValueFree(lease_new);
-    virJSONValueFree(leases_array_new);
-
     return rv;
 }
diff --git a/src/util/virdnsmasq.h b/src/util/virdnsmasq.h
index 4c14bc6ca7..e3814c2eb1 100644
--- a/src/util/virdnsmasq.h
+++ b/src/util/virdnsmasq.h
@@ -78,10 +78,14 @@ typedef enum {
 typedef struct _dnsmasqCaps dnsmasqCaps;
 typedef dnsmasqCaps *dnsmasqCapsPtr;
 
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(dnsmasqCaps, virObjectUnref);
+
 
 dnsmasqContext * dnsmasqContextNew(const char *network_name,
                                    const char *config_dir);
 void             dnsmasqContextFree(dnsmasqContext *ctx);
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(dnsmasqContext, dnsmasqContextFree);
+
 int              dnsmasqAddDhcpHost(dnsmasqContext *ctx,
                                     const char *mac,
                                     virSocketAddr *ip,
-- 
2.25.4




More information about the libvir-list mailing list