1b5975d6bSopenharmony_ciFrom 1f88976610d5bcc15ad58c9345848d736d64fd55 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 17:16:07 +0200
4b5975d6bSopenharmony_ciSubject: [PATCH] gregex: Do not try access the undefined match offsets if we
5b5975d6bSopenharmony_ci have no match
6b5975d6bSopenharmony_ci
7b5975d6bSopenharmony_ciIn case we're getting NO-MATCH "errors", we were still recomputing the
8b5975d6bSopenharmony_cimatch offsets and taking decisions based on that, that might lead to
9b5975d6bSopenharmony_ciundefined behavior.
10b5975d6bSopenharmony_ci
11b5975d6bSopenharmony_ciAvoid this by just returning early a FALSE result (but with no error) in
12b5975d6bSopenharmony_cicase there's no result to proceed on.
13b5975d6bSopenharmony_ci
14b5975d6bSopenharmony_ciFixes: #2741
15b5975d6bSopenharmony_ci---
16b5975d6bSopenharmony_ci glib/gregex.c      | 6 ++++++
17b5975d6bSopenharmony_ci glib/tests/regex.c | 6 ++++++
18b5975d6bSopenharmony_ci 2 files changed, 12 insertions(+)
19b5975d6bSopenharmony_ci
20b5975d6bSopenharmony_cidiff --git a/glib/gregex.c b/glib/gregex.c
21b5975d6bSopenharmony_ciindex 219d9cee34..f2a5b5fd1c 100644
22b5975d6bSopenharmony_ci--- a/glib/gregex.c
23b5975d6bSopenharmony_ci+++ b/glib/gregex.c
24b5975d6bSopenharmony_ci@@ -1073,6 +1073,12 @@ g_match_info_next (GMatchInfo  *match_info,
25b5975d6bSopenharmony_ci                    match_info->regex->pattern, match_error (match_info->matches));
26b5975d6bSopenharmony_ci       return FALSE;
27b5975d6bSopenharmony_ci     }
28b5975d6bSopenharmony_ci+  else if (match_info->matches == PCRE2_ERROR_NOMATCH)
29b5975d6bSopenharmony_ci+    {
30b5975d6bSopenharmony_ci+      /* We're done with this match info */
31b5975d6bSopenharmony_ci+      match_info->pos = -1;
32b5975d6bSopenharmony_ci+      return FALSE;
33b5975d6bSopenharmony_ci+    }
34b5975d6bSopenharmony_ci   else
35b5975d6bSopenharmony_ci     if (!recalc_match_offsets (match_info, error))
36b5975d6bSopenharmony_ci       return FALSE;
37b5975d6bSopenharmony_cidiff --git a/glib/tests/regex.c b/glib/tests/regex.c
38b5975d6bSopenharmony_ciindex 10daa7814a..291c21b4c7 100644
39b5975d6bSopenharmony_ci--- a/glib/tests/regex.c
40b5975d6bSopenharmony_ci+++ b/glib/tests/regex.c
41b5975d6bSopenharmony_ci@@ -1669,6 +1669,12 @@ test_class (void)
42b5975d6bSopenharmony_ci   res = g_match_info_next (match, NULL);
43b5975d6bSopenharmony_ci   g_assert (!res);
44b5975d6bSopenharmony_ci 
45b5975d6bSopenharmony_ci+  /* Accessing match again should not crash */
46b5975d6bSopenharmony_ci+  g_test_expect_message ("GLib", G_LOG_LEVEL_CRITICAL,
47b5975d6bSopenharmony_ci+                         "*match_info->pos >= 0*");
48b5975d6bSopenharmony_ci+  g_assert_false (g_match_info_next (match, NULL));
49b5975d6bSopenharmony_ci+  g_test_assert_expected_messages ();
50b5975d6bSopenharmony_ci+
51b5975d6bSopenharmony_ci   g_match_info_free (match);
52b5975d6bSopenharmony_ci   g_regex_unref (regex);
53b5975d6bSopenharmony_ci }
54b5975d6bSopenharmony_ci-- 
55b5975d6bSopenharmony_ciGitLab
56b5975d6bSopenharmony_ci
57