[glib: 2/8] guri: Simplify memory management in parse_host()




commit 17a53f2fc787f7cf6a82d4a491657b9de8bd28ed
Author: Philip Withnall <pwithnall endlessos org>
Date:   Wed Sep 30 17:46:02 2020 +0100

    guri: Simplify memory management in parse_host()
    
    This introduces no functional changes, but makes the memory ownership a
    little clearer and reduces the length of the code.
    
    Signed-off-by: Philip Withnall <pwithnall endlessos org>

 glib/guri.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)
---
diff --git a/glib/guri.c b/glib/guri.c
index 73258fdaf..6c5e579cb 100644
--- a/glib/guri.c
+++ b/glib/guri.c
@@ -511,7 +511,7 @@ parse_host (const gchar  *start,
             gchar       **out,
             GError      **error)
 {
-  gchar *decoded, *host;
+  gchar *decoded = NULL, *host;
   gchar *addr = NULL;
 
   if (*start == '[')
@@ -537,7 +537,7 @@ parse_host (const gchar  *start,
       if (!uri_normalize (&decoded, start, length, flags,
                           G_URI_ERROR_BAD_HOST, error))
         return FALSE;
-      host = decoded;
+      host = g_steal_pointer (&decoded);
       goto ok;
     }
 
@@ -559,18 +559,16 @@ parse_host (const gchar  *start,
     }
 
   if (g_hostname_is_non_ascii (decoded))
-    {
-      host = g_hostname_to_ascii (decoded);
-      g_free (decoded);
-    }
+    host = g_hostname_to_ascii (decoded);
   else
-    host = decoded;
+    host = g_steal_pointer (&decoded);
 
  ok:
   if (out)
-    *out = host;
-  else
-    g_free (host);
+    *out = g_steal_pointer (&host);
+  g_free (host);
+  g_free (decoded);
+
   return TRUE;
 }
 


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