From 8eeaba188272de496671763bc31f7fd592cb5ce0 Mon Sep 17 00:00:00 2001
From: Aleksander Morgado <aleksandermj@chromium.org>
Date: Wed, 24 Aug 2022 16:55:41 +0000
Subject: [PATCH] modem-helpers-ublox: rework +UIPADDR response parser

We setup all output variables with g_autofree and then use
g_steal_pointer() to return the needed ones.
---
 plugins/ublox/mm-modem-helpers-ublox.c | 65 ++++++++++----------------
 1 file changed, 24 insertions(+), 41 deletions(-)

diff --git a/plugins/ublox/mm-modem-helpers-ublox.c b/plugins/ublox/mm-modem-helpers-ublox.c
index 5cc035422..84a7cf272 100644
--- a/plugins/ublox/mm-modem-helpers-ublox.c
+++ b/plugins/ublox/mm-modem-helpers-ublox.c
@@ -228,11 +228,11 @@ mm_ublox_parse_uipaddr_response (const gchar  *response,
     g_autoptr(GMatchInfo)  match_info = NULL;
     GError                *inner_error = NULL;
     guint                  cid = 0;
-    gchar                 *if_name = NULL;
-    gchar                 *ipv4_address = NULL;
-    gchar                 *ipv4_subnet = NULL;
-    gchar                 *ipv6_global_address = NULL;
-    gchar                 *ipv6_link_local_address = NULL;
+    g_autofree gchar      *if_name = NULL;
+    g_autofree gchar      *ipv4_address = NULL;
+    g_autofree gchar      *ipv4_subnet = NULL;
+    g_autofree gchar      *ipv6_global_address = NULL;
+    g_autofree gchar      *ipv6_link_local_address = NULL;
 
     /* Response may be e.g.:
      * +UIPADDR: 1,"ccinet0","5.168.120.13","255.255.255.0","",""
@@ -245,61 +245,44 @@ mm_ublox_parse_uipaddr_response (const gchar  *response,
     g_assert (r != NULL);
 
     g_regex_match_full (r, response, strlen (response), 0, 0, &match_info, &inner_error);
-    if (inner_error)
-        goto out;
+    if (inner_error) {
+        g_propagate_error (error, inner_error);
+        return FALSE;
+    }
 
     if (!g_match_info_matches (match_info)) {
-        inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_INVALID_ARGS, "Couldn't match +UIPADDR response");
-        goto out;
+        g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_INVALID_ARGS, "Couldn't match +UIPADDR response");
+        return FALSE;
     }
 
     if (out_cid && !mm_get_uint_from_match_info (match_info, 1, &cid)) {
-        inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED, "Error parsing cid");
-        goto out;
+        g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED, "Error parsing cid");
+        return FALSE;
     }
 
     if (out_if_name && !(if_name = mm_get_string_unquoted_from_match_info (match_info, 2))) {
-        inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED, "Error parsing interface name");
-        goto out;
+        g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED, "Error parsing interface name");
+        return FALSE;
     }
 
     /* Remaining strings are optional */
-
-    if (out_ipv4_address)
-        ipv4_address = mm_get_string_unquoted_from_match_info (match_info, 3);
-
-    if (out_ipv4_subnet)
-        ipv4_subnet = mm_get_string_unquoted_from_match_info (match_info, 4);
-
-    if (out_ipv6_global_address)
-        ipv6_global_address = mm_get_string_unquoted_from_match_info (match_info, 5);
-
-    if (out_ipv6_link_local_address)
-        ipv6_link_local_address = mm_get_string_unquoted_from_match_info (match_info, 6);
-
-out:
-    if (inner_error) {
-        g_free (if_name);
-        g_free (ipv4_address);
-        g_free (ipv4_subnet);
-        g_free (ipv6_global_address);
-        g_free (ipv6_link_local_address);
-        g_propagate_error (error, inner_error);
-        return FALSE;
-    }
+    ipv4_address = mm_get_string_unquoted_from_match_info (match_info, 3);
+    ipv4_subnet = mm_get_string_unquoted_from_match_info (match_info, 4);
+    ipv6_global_address = mm_get_string_unquoted_from_match_info (match_info, 5);
+    ipv6_link_local_address = mm_get_string_unquoted_from_match_info (match_info, 6);
 
     if (out_cid)
         *out_cid = cid;
     if (out_if_name)
-        *out_if_name = if_name;
+        *out_if_name = g_steal_pointer (&if_name);
     if (out_ipv4_address)
-        *out_ipv4_address = ipv4_address;
+        *out_ipv4_address = g_steal_pointer (&ipv4_address);
     if (out_ipv4_subnet)
-        *out_ipv4_subnet = ipv4_subnet;
+        *out_ipv4_subnet = g_steal_pointer (&ipv4_subnet);
     if (out_ipv6_global_address)
-        *out_ipv6_global_address = ipv6_global_address;
+        *out_ipv6_global_address = g_steal_pointer (&ipv6_global_address);
     if (out_ipv6_link_local_address)
-        *out_ipv6_link_local_address = ipv6_link_local_address;
+        *out_ipv6_link_local_address = g_steal_pointer (&ipv6_link_local_address);
     return TRUE;
 }
 
-- 
GitLab
