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.
This commit is contained in:
Aleksander Morgado
2022-09-05 17:37:14 +00:00
parent 9bbc768666
commit 8eeaba1882
+31 -48
View File
@@ -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 (!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;
}
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;
}
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;
}
/* 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;
}
if (!g_match_info_matches (match_info)) {
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)) {
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))) {
g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED, "Error parsing interface name");
return FALSE;
}
/* Remaining strings are optional */
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;
}