1b5975d6bSopenharmony_ciFrom 11521972f4d345d9a3f68df719f5980085197e47 Mon Sep 17 00:00:00 2001
2b5975d6bSopenharmony_ciFrom: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= <mail@3v1n0.net>
3b5975d6bSopenharmony_ciDate: Tue, 6 Sep 2022 18:26:12 +0200
4b5975d6bSopenharmony_ciSubject: [PATCH] gregex: Handle the case we need to re-allocate the match data
5b5975d6bSopenharmony_ci
6b5975d6bSopenharmony_ciIn case PCRE2 returns an empty match
7b5975d6bSopenharmony_ci
8b5975d6bSopenharmony_ciThis can be easily tested by initializing the initial match data to a
9b5975d6bSopenharmony_civalue that is less than the expected match values (e.g. by calling
10b5975d6bSopenharmony_cipcre2_match_data_create (1, NULL)), but we can't do it in our tests
11b5975d6bSopenharmony_ciwithout bigger changes.
12b5975d6bSopenharmony_ci---
13b5975d6bSopenharmony_ci glib/gregex.c | 15 ++++++++++++++-
14b5975d6bSopenharmony_ci 1 file changed, 14 insertions(+), 1 deletion(-)
15b5975d6bSopenharmony_ci
16b5975d6bSopenharmony_cidiff --git a/glib/gregex.c b/glib/gregex.c
17b5975d6bSopenharmony_ciindex b886b24e2a..84c4245753 100644
18b5975d6bSopenharmony_ci--- a/glib/gregex.c
19b5975d6bSopenharmony_ci+++ b/glib/gregex.c
20b5975d6bSopenharmony_ci@@ -1027,7 +1027,7 @@ g_match_info_next (GMatchInfo  *match_info,
21b5975d6bSopenharmony_ci {
22b5975d6bSopenharmony_ci   gint prev_match_start;
23b5975d6bSopenharmony_ci   gint prev_match_end;
24b5975d6bSopenharmony_ci-  gint opts;
25b5975d6bSopenharmony_ci+  uint32_t opts;
26b5975d6bSopenharmony_ci 
27b5975d6bSopenharmony_ci   g_return_val_if_fail (match_info != NULL, FALSE);
28b5975d6bSopenharmony_ci   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
29b5975d6bSopenharmony_ci@@ -1075,6 +1075,19 @@ g_match_info_next (GMatchInfo  *match_info,
30b5975d6bSopenharmony_ci                    match_info->regex->pattern, match_error (match_info->matches));
31b5975d6bSopenharmony_ci       return FALSE;
32b5975d6bSopenharmony_ci     }
33b5975d6bSopenharmony_ci+  else if (match_info->matches == 0)
34b5975d6bSopenharmony_ci+    {
35b5975d6bSopenharmony_ci+      /* info->offsets is too small. */
36b5975d6bSopenharmony_ci+      match_info->n_offsets *= 2;
37b5975d6bSopenharmony_ci+      match_info->offsets = g_realloc_n (match_info->offsets,
38b5975d6bSopenharmony_ci+                                         match_info->n_offsets,
39b5975d6bSopenharmony_ci+                                         sizeof (gint));
40b5975d6bSopenharmony_ci+
41b5975d6bSopenharmony_ci+      pcre2_match_data_free (match_info->match_data);
42b5975d6bSopenharmony_ci+      match_info->match_data = pcre2_match_data_create (match_info->n_offsets, NULL);
43b5975d6bSopenharmony_ci+
44b5975d6bSopenharmony_ci+      return g_match_info_next (match_info, error);
45b5975d6bSopenharmony_ci+    }
46b5975d6bSopenharmony_ci   else if (match_info->matches == PCRE2_ERROR_NOMATCH)
47b5975d6bSopenharmony_ci     {
48b5975d6bSopenharmony_ci       /* We're done with this match info */
49b5975d6bSopenharmony_ci-- 
50b5975d6bSopenharmony_ciGitLab
51b5975d6bSopenharmony_ci
52