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

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

 



On 2/23/21 12:24 PM, Kristina Hanicova wrote:
Signed-off-by: Kristina Hanicova <khanicov@xxxxxxxxxx>
---
  src/conf/networkcommon_conf.c | 14 +++++---------
  1 file changed, 5 insertions(+), 9 deletions(-)

diff --git a/src/conf/networkcommon_conf.c b/src/conf/networkcommon_conf.c
index 26eeb6dbda..e82dbc3d3d 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,7 +284,7 @@ int
  virNetDevIPRouteFormat(virBufferPtr buf,
                         const virNetDevIPRoute *def)
  {
-    char *addr = NULL;
+    g_autofree char *addr = NULL;
virBufferAddLit(buf, "<route"); @@ -311,7 +308,6 @@ virNetDevIPRouteFormat(virBufferPtr buf,
      if (!(addr = virSocketAddrFormat(&def->gateway)))
          return -1;
      virBufferAsprintf(buf, " gateway='%s'", addr);
-    VIR_FREE(addr);

This one is a bit more complicated, because "addr" is re-used after being freed (twice - first used for address, then for netmask, and finally for gateway), and we've made the rule that an auto-freed pointer should never be VIR_FREED

So instead of just making the existing pointer g_autofree and eliminating its final free, we need to create separate pointers for each use:

g_autofree char *address = NULL;
g_autofree char *netmask = NULL;
g_autofree char *gataeway = NULL;

and then use each in the appropriate place.

(if we're lucky, the compiler/optimizer will even be smart enough to figure out that the three things are never used at the same time (? depending on when the call to the autofree function is injected), and internally merge them into a single variable.)

if (def->has_metric && def->metric > 0)
          virBufferAsprintf(buf, " metric='%u'", def->metric);





[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