From c1306126c3f12c16ad62dd2553132f64a28ca607 Mon Sep 17 00:00:00 2001
From: Philip Hazel <Philip.Hazel@gmail.com>
Date: Sun, 19 Nov 2023 17:18:07 +0000
Subject: [PATCH] Fix 32-bit quantifier following a character larger than the
 maximum UTF character.

---
 src/pcre2_compile.c      | 11 ++++++++---
 testdata/testinput12     |  6 ++++++
 testdata/testoutput12-16 |  7 +++++++
 testdata/testoutput12-32 |  7 +++++++
 4 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/src/pcre2_compile.c b/src/pcre2_compile.c
index b3e4969..fdaf2ad 100644
--- a/src/pcre2_compile.c
+++ b/src/pcre2_compile.c
@@ -2781,6 +2781,7 @@ uint32_t *verbstartptr = NULL;
 uint32_t *previous_callout = NULL;
 uint32_t *parsed_pattern = cb->parsed_pattern;
 uint32_t *parsed_pattern_end = cb->parsed_pattern_end;
+uint32_t *this_parsed_item = NULL;
 uint32_t meta_quantifier = 0;
 uint32_t add_after_mark = 0;
 uint32_t extra_options = cb->cx->extra_options;
@@ -2866,10 +2867,11 @@ while (ptr < ptrend)
   uint32_t set, unset, *optset;
   uint32_t terminator;
   uint32_t prev_meta_quantifier;
+  uint32_t *prev_parsed_item = this_parsed_item; 
   BOOL prev_okquantifier;
   PCRE2_SPTR tempptr;
   PCRE2_SIZE offset;
-
+  
   if (parsed_pattern >= parsed_pattern_end)
     {
     errorcode = ERR63;  /* Internal error (parsed pattern overflow) */
@@ -2881,6 +2883,10 @@ while (ptr < ptrend)
     errorcode = ERR19;
     goto FAILED;        /* Parentheses too deeply nested */
     }
+    
+  /* Remember where this item started */
+
+  this_parsed_item = parsed_pattern;
 
   /* Get next input character, save its position for callout handling. */
 
@@ -3173,7 +3179,6 @@ while (ptr < ptrend)
     continue;  /* Next character in pattern */
     }
 
-
   /* Process the next item in the main part of a pattern. */
 
   switch(c)
@@ -3450,7 +3455,7 @@ while (ptr < ptrend)
     wrapping it in non-capturing brackets, but we have to allow for a preceding
     (*MARK) for when (*ACCEPT) has an argument. */
 
-    if (parsed_pattern[-1] == META_ACCEPT)
+    if (*prev_parsed_item == META_ACCEPT)
       {
       uint32_t *p;
       for (p = parsed_pattern - 1; p >= verbstartptr; p--) p[1] = p[0];
diff --git a/testdata/testinput12 b/testdata/testinput12
index 7a85eb5..1e552e6 100644
--- a/testdata/testinput12
+++ b/testdata/testinput12
@@ -560,4 +560,10 @@
 
 # ---------------------------------------------------- 
 
+# Quantifier after a literal that has the value of META_ACCEPT (not UTF). This
+# fails in 16-bit mode, but is OK for 32-bit.
+
+/\x{802a0000}*/
+    \x{802a0000}\x{802a0000}
+
 # End of testinput12
diff --git a/testdata/testoutput12-16 b/testdata/testoutput12-16
index 9867632..8cbc13d 100644
--- a/testdata/testoutput12-16
+++ b/testdata/testoutput12-16
@@ -1803,4 +1803,11 @@ No match
 
 # ---------------------------------------------------- 
 
+# Quantifier after a literal that has the value of META_ACCEPT (not UTF). This
+# fails in 16-bit mode, but is OK for 32-bit.
+
+/\x{802a0000}*/
+Failed: error 134 at offset 11: character code point value in \x{} or \o{} is too large
+    \x{802a0000}\x{802a0000}
+
 # End of testinput12
diff --git a/testdata/testoutput12-32 b/testdata/testoutput12-32
index 3a20dd4..1a98b4b 100644
--- a/testdata/testoutput12-32
+++ b/testdata/testoutput12-32
@@ -1801,4 +1801,11 @@ No match
 
 # ---------------------------------------------------- 
 
+# Quantifier after a literal that has the value of META_ACCEPT (not UTF). This
+# fails in 16-bit mode, but is OK for 32-bit.
+
+/\x{802a0000}*/
+    \x{802a0000}\x{802a0000}
+ 0: \x{802a0000}\x{802a0000}
+
 # End of testinput12
-- 
2.33.0

