1b5975d6bSopenharmony_ciFrom 406f85a48f1ec41cda15ae617a979f7df749cb27 Mon Sep 17 00:00:00 2001
2b5975d6bSopenharmony_ciFrom: Aleksei Rybalkin <aleksei@rybalkin.org>
3b5975d6bSopenharmony_ciDate: Sun, 20 Aug 2023 16:33:53 +0200
4b5975d6bSopenharmony_ciSubject: [PATCH 1/2] gregex: if JIT stack limit is reached, fall back to
5b5975d6bSopenharmony_ci interpretive matching
6b5975d6bSopenharmony_ci 
7b5975d6bSopenharmony_ciConflict:Move large_test_string to fix declaration-after-statement
8b5975d6bSopenharmony_ciReference:https://gitlab.gnome.org/GNOME/glib/-/commit/406f85a48f1ec41cda15ae617a979f7df749cb27
9b5975d6bSopenharmony_ci 
10b5975d6bSopenharmony_ci---
11b5975d6bSopenharmony_ci glib/gregex.c      | 13 ++++++++++---
12b5975d6bSopenharmony_ci glib/tests/regex.c | 10 +++++++++-
13b5975d6bSopenharmony_ci 2 files changed, 19 insertions(+), 4 deletions(-)
14b5975d6bSopenharmony_ci 
15b5975d6bSopenharmony_cidiff --git a/glib/gregex.c b/glib/gregex.c
16b5975d6bSopenharmony_ciindex 5ce034db41..1b3ee02f30 100644
17b5975d6bSopenharmony_ci--- a/glib/gregex.c
18b5975d6bSopenharmony_ci+++ b/glib/gregex.c
19b5975d6bSopenharmony_ci@@ -484,8 +484,6 @@ translate_match_error (gint errcode)
20b5975d6bSopenharmony_ci       /* not used by pcre2_match() */
21b5975d6bSopenharmony_ci       break;
22b5975d6bSopenharmony_ci     case PCRE2_ERROR_MATCHLIMIT:
23b5975d6bSopenharmony_ci-    case PCRE2_ERROR_JIT_STACKLIMIT:
24b5975d6bSopenharmony_ci-      return _("backtracking limit reached");
25b5975d6bSopenharmony_ci     case PCRE2_ERROR_CALLOUT:
26b5975d6bSopenharmony_ci       /* callouts are not implemented */
27b5975d6bSopenharmony_ci       break;
28b5975d6bSopenharmony_ci@@ -1107,8 +1105,17 @@ g_match_info_next (GMatchInfo  *match_info,
29b5975d6bSopenharmony_ci                                              opts,
30b5975d6bSopenharmony_ci                                              match_info->match_data,
31b5975d6bSopenharmony_ci                                              match_info->match_context);
32b5975d6bSopenharmony_ci+      /* if the JIT stack limit was reached, fall back to non-JIT matching in
33b5975d6bSopenharmony_ci+       * the next conditional statement */
34b5975d6bSopenharmony_ci+      if (match_info->matches == PCRE2_ERROR_JIT_STACKLIMIT)
35b5975d6bSopenharmony_ci+        {
36b5975d6bSopenharmony_ci+          g_info ("PCRE2 JIT stack limit reached, falling back to "
37b5975d6bSopenharmony_ci+                  "non-optimized matching.");
38b5975d6bSopenharmony_ci+          opts |= PCRE2_NO_JIT;
39b5975d6bSopenharmony_ci+          jit_status = JIT_STATUS_DISABLED;
40b5975d6bSopenharmony_ci+        }
41b5975d6bSopenharmony_ci     }
42b5975d6bSopenharmony_ci-  else
43b5975d6bSopenharmony_ci+  if (jit_status != JIT_STATUS_ENABLED)
44b5975d6bSopenharmony_ci     {
45b5975d6bSopenharmony_ci       match_info->matches = pcre2_match (match_info->regex->pcre_re,
46b5975d6bSopenharmony_ci                                          (PCRE2_SPTR8) match_info->string,
47b5975d6bSopenharmony_cidiff --git a/glib/tests/regex.c b/glib/tests/regex.c
48b5975d6bSopenharmony_ciindex 821fc59608..f18db483c2 100644
49b5975d6bSopenharmony_ci--- a/glib/tests/regex.c
50b5975d6bSopenharmony_ci+++ b/glib/tests/regex.c
51b5975d6bSopenharmony_ci@@ -51,8 +51,9 @@
52b5975d6bSopenharmony_ci /* A random value use to mark untouched integer variables. */
53b5975d6bSopenharmony_ci #define UNTOUCHED -559038737
54b5975d6bSopenharmony_ci 
55b5975d6bSopenharmony_ci-/* A length of the test string in JIT stack test */
56b5975d6bSopenharmony_ci+/* Lengths of test strings in JIT stack tests */
57b5975d6bSopenharmony_ci #define TEST_STRING_LEN 20000
58b5975d6bSopenharmony_ci+#define LARGE_TEST_STRING_LEN 200000
59b5975d6bSopenharmony_ci 
60b5975d6bSopenharmony_ci static gint total;
61b5975d6bSopenharmony_ci 
62b5975d6bSopenharmony_ci@@ -2485,6 +2486,7 @@ int
63b5975d6bSopenharmony_ci main (int argc, char *argv[])
64b5975d6bSopenharmony_ci {
65b5975d6bSopenharmony_ci   char test_string[TEST_STRING_LEN];
66b5975d6bSopenharmony_ci+  char large_test_string[LARGE_TEST_STRING_LEN];
67b5975d6bSopenharmony_ci   setlocale (LC_ALL, "");
68b5975d6bSopenharmony_ci 
69b5975d6bSopenharmony_ci   g_test_init (&argc, &argv, NULL);
70b5975d6bSopenharmony_ci@@ -2711,6 +2713,12 @@ G_GNUC_END_IGNORE_DEPRECATIONS
71b5975d6bSopenharmony_ci   test_string[TEST_STRING_LEN - 1] = '\0';
72b5975d6bSopenharmony_ci   TEST_MATCH_SIMPLE ("^(?:[ \t\n]|[^[:cntrl:]])*$", test_string, 0, 0, TRUE);
73b5975d6bSopenharmony_ci 
74b5975d6bSopenharmony_ci+  /* Test that gregex falls back to unoptimized matching when reaching the JIT
75b5975d6bSopenharmony_ci+   * compiler stack limit */
76b5975d6bSopenharmony_ci+  memset (large_test_string, '*', LARGE_TEST_STRING_LEN);
77b5975d6bSopenharmony_ci+  large_test_string[LARGE_TEST_STRING_LEN - 1] = '\0';
78b5975d6bSopenharmony_ci+  TEST_MATCH_SIMPLE ("^(?:[ \t\n]|[^[:cntrl:]])*$", large_test_string, 0, 0, TRUE);
79b5975d6bSopenharmony_ci+
80b5975d6bSopenharmony_ci   /* TEST_MATCH(pattern, compile_opts, match_opts, string,
81b5975d6bSopenharmony_ci    * 		string_len, start_position, match_opts2, expected) */
82b5975d6bSopenharmony_ci   TEST_MATCH("a", 0, 0, "a", -1, 0, 0, TRUE);
83b5975d6bSopenharmony_ci-- 
84b5975d6bSopenharmony_ciGitLab
85b5975d6bSopenharmony_ci 
86b5975d6bSopenharmony_ci 
87b5975d6bSopenharmony_ciFrom 986fa3fdad5155924b17dbde16811d017a6413da Mon Sep 17 00:00:00 2001
88b5975d6bSopenharmony_ciFrom: Philip Withnall <philip@tecnocode.co.uk>
89b5975d6bSopenharmony_ciDate: Mon, 21 Aug 2023 10:19:43 +0000
90b5975d6bSopenharmony_ciSubject: [PATCH 2/2] Apply 2 suggestion(s) to 1 file(s)
91b5975d6bSopenharmony_ci 
92b5975d6bSopenharmony_ciConflict:NA
93b5975d6bSopenharmony_ciReference:https://gitlab.gnome.org/GNOME/glib/-/commit/986fa3fdad5155924b17dbde16811d017a6413da
94b5975d6bSopenharmony_ci 
95b5975d6bSopenharmony_ci---
96b5975d6bSopenharmony_ci glib/gregex.c | 5 +++--
97b5975d6bSopenharmony_ci 1 file changed, 3 insertions(+), 2 deletions(-)
98b5975d6bSopenharmony_ci 
99b5975d6bSopenharmony_cidiff --git a/glib/gregex.c b/glib/gregex.c
100b5975d6bSopenharmony_ciindex 1b3ee02f30..b37a5e04c7 100644
101b5975d6bSopenharmony_ci--- a/glib/gregex.c
102b5975d6bSopenharmony_ci+++ b/glib/gregex.c
103b5975d6bSopenharmony_ci@@ -1109,12 +1109,13 @@ g_match_info_next (GMatchInfo  *match_info,
104b5975d6bSopenharmony_ci        * the next conditional statement */
105b5975d6bSopenharmony_ci       if (match_info->matches == PCRE2_ERROR_JIT_STACKLIMIT)
106b5975d6bSopenharmony_ci         {
107b5975d6bSopenharmony_ci-          g_info ("PCRE2 JIT stack limit reached, falling back to "
108b5975d6bSopenharmony_ci-                  "non-optimized matching.");
109b5975d6bSopenharmony_ci+          g_debug ("PCRE2 JIT stack limit reached, falling back to "
110b5975d6bSopenharmony_ci+                   "non-optimized matching.");
111b5975d6bSopenharmony_ci           opts |= PCRE2_NO_JIT;
112b5975d6bSopenharmony_ci           jit_status = JIT_STATUS_DISABLED;
113b5975d6bSopenharmony_ci         }
114b5975d6bSopenharmony_ci     }
115b5975d6bSopenharmony_ci+
116b5975d6bSopenharmony_ci   if (jit_status != JIT_STATUS_ENABLED)
117b5975d6bSopenharmony_ci     {
118b5975d6bSopenharmony_ci       match_info->matches = pcre2_match (match_info->regex->pcre_re,
119b5975d6bSopenharmony_ci-- 
120b5975d6bSopenharmony_ciGitLab