[PATCH v2 1/3] networkcommon_conf: Use g_autofree where possible

Kristina Hanicova khanicov at redhat.com
Thu Feb 25 11:51:55 UTC 2021


Signed-off-by: Kristina Hanicova <khanicov at redhat.com>
---
 src/conf/networkcommon_conf.c | 30 +++++++++++++-----------------
 1 file changed, 13 insertions(+), 17 deletions(-)

diff --git a/src/conf/networkcommon_conf.c b/src/conf/networkcommon_conf.c
index 26eeb6dbda..430d48a997 100644
--- a/src/conf/networkcommon_conf.c
+++ b/src/conf/networkcommon_conf.c
@@ -228,9 +228,10 @@ virNetDevIPRouteParseXML(const char *errorDetail,
 
     virNetDevIPRoutePtr def = NULL;
     VIR_XPATH_NODE_AUTORESTORE(ctxt)
-    char *family = NULL;
-    char *address = NULL, *netmask = NULL;
-    char *gateway = NULL;
+    g_autofree char *family = NULL;
+    g_autofree char *address = NULL;
+    g_autofree char *netmask = NULL;
+    g_autofree char *gateway = NULL;
     unsigned long prefix = 0, metric = 0;
     int prefixRc, metricRc;
     bool hasPrefix = false;
@@ -276,10 +277,6 @@ virNetDevIPRouteParseXML(const char *errorDetail,
                                  hasMetric);
 
  cleanup:
-    VIR_FREE(family);
-    VIR_FREE(address);
-    VIR_FREE(netmask);
-    VIR_FREE(gateway);
     return def;
 }
 
@@ -287,31 +284,30 @@ int
 virNetDevIPRouteFormat(virBufferPtr buf,
                        const virNetDevIPRoute *def)
 {
-    char *addr = NULL;
+    g_autofree char *address = NULL;
+    g_autofree char *netmask = NULL;
+    g_autofree char *gateway = NULL;
 
     virBufferAddLit(buf, "<route");
 
     if (def->family)
         virBufferAsprintf(buf, " family='%s'", def->family);
 
-    if (!(addr = virSocketAddrFormat(&def->address)))
+    if (!(address = virSocketAddrFormat(&def->address)))
         return -1;
-    virBufferAsprintf(buf, " address='%s'", addr);
-    VIR_FREE(addr);
+    virBufferAsprintf(buf, " address='%s'", address);
 
     if (VIR_SOCKET_ADDR_VALID(&def->netmask)) {
-        if (!(addr = virSocketAddrFormat(&def->netmask)))
+        if (!(netmask = virSocketAddrFormat(&def->netmask)))
             return -1;
-        virBufferAsprintf(buf, " netmask='%s'", addr);
-        VIR_FREE(addr);
+        virBufferAsprintf(buf, " netmask='%s'", netmask);
     }
     if (def->has_prefix)
         virBufferAsprintf(buf, " prefix='%u'", def->prefix);
 
-    if (!(addr = virSocketAddrFormat(&def->gateway)))
+    if (!(gateway = virSocketAddrFormat(&def->gateway)))
         return -1;
-    virBufferAsprintf(buf, " gateway='%s'", addr);
-    VIR_FREE(addr);
+    virBufferAsprintf(buf, " gateway='%s'", gateway);
 
     if (def->has_metric && def->metric > 0)
         virBufferAsprintf(buf, " metric='%u'", def->metric);
-- 
2.29.2




More information about the libvir-list mailing list