core: port GRegex/GMatchInfo to use autoptr()

The behavior of GRegex changed in 2.73.2 once it was ported from pcre1
to pcre2. In some cases it was made more strict, which is fine, in
other cases it exposed some change in how it behaves on certain
matches that is not extremely clear whether it's ok or not.

See https://gitlab.gnome.org/GNOME/glib/-/issues/2729
See https://gitlab.freedesktop.org/mobile-broadband/ModemManager/-/issues/601
See https://gitlab.freedesktop.org/mobile-broadband/ModemManager/-/issues/621

Either way, one thing that was assumed was that initializing all
GRegex/GMatchInfo variables to NULL and making sure they're NULL
before they're initialized by glib (especially the GMatchInfo) was a
good and safer approach.

So, whenever possible, g_autoptr() is used to cleanup the allocated
GMatchInfo/GRegex variables, and otherwise, g_clear_pointer() is used
to ensure that no free/unref is attempted unless the given variable is
not NULL, and also so that the variable is reseted to NULL after being
disposed.
This commit is contained in:
Aleksander Morgado
2022-09-05 17:33:11 +00:00
parent b2a186b5c8
commit 74fc5baca2
33 changed files with 850 additions and 1268 deletions
@@ -1192,18 +1192,17 @@ test_smong_response_no_match (void)
/* Test ^SLCC URCs */
static void
common_test_slcc_urc (const gchar *urc,
common_test_slcc_urc (const gchar *urc,
const MMCallInfo *expected_call_info_list,
guint expected_call_info_list_size)
guint expected_call_info_list_size)
{
GError *error = NULL;
GRegex *slcc_regex = NULL;
gboolean result;
GMatchInfo *match_info = NULL;
gchar *str;
GList *call_info_list = NULL;
GList *l;
g_autoptr(GRegex) slcc_regex = NULL;
g_autoptr(GMatchInfo) match_info = NULL;
g_autofree gchar *str = NULL;
GError *error = NULL;
GList *call_info_list = NULL;
GList *l;
gboolean result;
slcc_regex = mm_cinterion_get_slcc_regex ();
@@ -1230,8 +1229,8 @@ common_test_slcc_urc (const gchar *urc,
for (l = call_info_list; l; l = g_list_next (l)) {
const MMCallInfo *call_info = (const MMCallInfo *)(l->data);
gboolean found = FALSE;
guint i;
gboolean found = FALSE;
guint i;
g_debug ("call at index %u: direction %s, state %s, number %s",
call_info->index,
@@ -1248,10 +1247,6 @@ common_test_slcc_urc (const gchar *urc,
g_assert (found);
}
g_match_info_free (match_info);
g_regex_unref (slcc_regex);
g_free (str);
mm_cinterion_call_info_list_free (call_info_list);
}
@@ -1322,12 +1317,12 @@ common_test_ctzu_urc (const gchar *urc,
gint expected_offset,
gint expected_dst_offset)
{
GError *error = NULL;
GRegex *ctzu_regex = NULL;
gboolean result;
GMatchInfo *match_info = NULL;
gchar *iso8601;
MMNetworkTimezone *tz = NULL;
g_autoptr(GRegex) ctzu_regex = NULL;
g_autoptr(GMatchInfo) match_info = NULL;
g_autofree gchar *iso8601 = NULL;
GError *error = NULL;
gboolean result;
MMNetworkTimezone *tz = NULL;
ctzu_regex = mm_cinterion_get_ctzu_regex ();
@@ -1342,7 +1337,6 @@ common_test_ctzu_urc (const gchar *urc,
g_assert (iso8601);
g_assert_cmpstr (expected_iso8601, ==, iso8601);
g_free (iso8601);
g_assert (tz);
g_assert_cmpint (expected_offset, ==, mm_network_timezone_get_offset (tz));
@@ -1351,8 +1345,6 @@ common_test_ctzu_urc (const gchar *urc,
g_assert_cmpuint ((guint)expected_dst_offset, ==, mm_network_timezone_get_dst_offset (tz));
g_object_unref (tz);
g_match_info_free (match_info);
g_regex_unref (ctzu_regex);
}
static void