From 542cb11242cfc9be9b6218965751bfbb13a8b6a2 Mon Sep 17 00:00:00 2001
From: Zoltan Herczeg <hzmester@freemail.hu>
Date: Wed, 27 Dec 2023 08:27:17 +0000
Subject: [PATCH] Fix incorrect class character matches in JIT

---
 src/pcre2_jit_compile.c | 8 ++++++++
 src/pcre2_jit_test.c    | 1 +
 2 files changed, 9 insertions(+)

diff --git a/src/pcre2_jit_compile.c b/src/pcre2_jit_compile.c
index 160b448..6d0d9a6 100644
--- a/src/pcre2_jit_compile.c
+++ b/src/pcre2_jit_compile.c
@@ -7938,6 +7938,7 @@ while (*cc != XCL_END)
 
       jump = CMP(SLJIT_NOT_EQUAL, typereg, 0, SLJIT_IMM, ucp_Cf - ucp_Ll);
 
+      c = charoffset;
       /* In case of ucp_Cf, we overwrite the result. */
       SET_CHAR_OFFSET(0x2066);
       OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x2069 - 0x2066);
@@ -7949,6 +7950,9 @@ while (*cc != XCL_END)
       OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x180e - 0x2066);
       OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
 
+      /* Restore charoffset */
+      SET_CHAR_OFFSET(c);
+
       JUMPHERE(jump);
       jump = CMP(SLJIT_ZERO ^ invertcmp, TMP2, 0, SLJIT_IMM, 0);
       break;
@@ -7964,6 +7968,7 @@ while (*cc != XCL_END)
 
       jump = CMP(SLJIT_NOT_EQUAL, typereg, 0, SLJIT_IMM, ucp_Cf - ucp_Ll);
 
+      c = charoffset;
       /* In case of ucp_Cf, we overwrite the result. */
       SET_CHAR_OFFSET(0x2066);
       OP2(SLJIT_SUB | SLJIT_SET_LESS_EQUAL, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x2069 - 0x2066);
@@ -7972,6 +7977,9 @@ while (*cc != XCL_END)
       OP2(SLJIT_SUB | SLJIT_SET_Z, SLJIT_UNUSED, 0, TMP1, 0, SLJIT_IMM, 0x061c - 0x2066);
       OP_FLAGS(SLJIT_OR, TMP2, 0, SLJIT_EQUAL);
 
+      /* Restore charoffset */
+      SET_CHAR_OFFSET(c);
+
       JUMPHERE(jump);
       jump = CMP(SLJIT_ZERO ^ invertcmp, TMP2, 0, SLJIT_IMM, 0);
       break;
diff --git a/src/pcre2_jit_test.c b/src/pcre2_jit_test.c
index 801929b..f6646b7 100644
--- a/src/pcre2_jit_test.c
+++ b/src/pcre2_jit_test.c
@@ -412,6 +412,7 @@ static struct regression_test_case regression_test_cases[] = {
 	{ PCRE2_UCP, 0, 0, 0 | F_PROPERTY, "[a-b\\s]{2,5}[^a]", "AB  baaa" },
 	{ CMUP, 0, 0, 0, "[^S]\\B", "\xe2\x80\x8a" },
        { MUP, 0, 0, 0 | F_NOMATCH, "[^\\p{Hangul}\\p{Z}]", " " },
+	{ MUP, 0, 0, 0 | F_NOMATCH, "[^[:print:]\\x{f6f6}]", "\xef\x9b\xb6" },
 
 	/* Possible empty brackets. */
 	{ MU, A, 0, 0, "(?:|ab||bc|a)+d", "abcxabcabd" },
-- 
2.33.0

