From 57ee073252dc826dbe412846a83421d2bb4483bc Mon Sep 17 00:00:00 2001
From: Philip Hazel <Philip.Hazel@gmail.com>
Date: Wed, 22 Nov 2023 11:34:27 +0000
Subject: [PATCH] Fix bad patch in 05206d66. The interpreter was handling
 NOTEOL incorrectly in general after trying to fix it in invalid UTF subjects.

---
 src/pcre2_intmodedep.h | 3 ++-
 src/pcre2_match.c      | 7 +++----
 testdata/testinput2    | 4 ++++
 testdata/testoutput2   | 6 ++++++
 4 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/src/pcre2_intmodedep.h b/src/pcre2_intmodedep.h
index 5e7e10d..423764d 100644
--- a/src/pcre2_intmodedep.h
+++ b/src/pcre2_intmodedep.h
@@ -880,7 +880,8 @@ typedef struct match_block {
   PCRE2_SPTR start_code;          /* For use when recursing */
   PCRE2_SPTR start_subject;       /* Start of the subject string */
   PCRE2_SPTR check_subject;       /* Where UTF-checked from */
-  PCRE2_SPTR end_subject;         /* End of the subject string */
+  PCRE2_SPTR end_subject;         /* Usable end of the subject string */
+  PCRE2_SPTR true_end_subject;    /* Actual end of the subject string */
   PCRE2_SPTR end_match_ptr;       /* Subject position at end match */
   PCRE2_SPTR start_used_ptr;      /* Earliest consulted character */
   PCRE2_SPTR last_used_ptr;       /* Latest consulted character */
diff --git a/src/pcre2_match.c b/src/pcre2_match.c
index ea03976..c5e84ce 100644
--- a/src/pcre2_match.c
+++ b/src/pcre2_match.c
@@ -6076,12 +6076,10 @@ fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode,
     if ((mb->poptions & PCRE2_DOLLAR_ENDONLY) == 0) goto ASSERT_NL_OR_EOS;
 
     /* Fall through */
-    /* Unconditional end of subject assertion (\z). We must check NOTEOL
-    because it gets set for invalid UTF fragments. */
+    /* Unconditional end of subject assertion (\z). */
 
     case OP_EOD:
-    if (Feptr < mb->end_subject || (mb->moptions & PCRE2_NOTEOL) != 0)
-      RRETURN(MATCH_NOMATCH);
+    if (Feptr < mb->true_end_subject) RRETURN(MATCH_NOMATCH);
     if (mb->partial != 0)
       {
       mb->hitend = TRUE;
@@ -6891,6 +6889,7 @@ mb->callout_data = mcontext->callout_data;
 mb->start_subject = subject;
 mb->start_offset = start_offset;
 mb->end_subject = end_subject;
+mb->true_end_subject = true_end_subject;
 mb->hasthen = (re->flags & PCRE2_HASTHEN) != 0;
 mb->allowemptypartial = (re->max_lookbehind > 0) ||
     (re->flags & PCRE2_MATCH_EMPTY) != 0;
diff --git a/testdata/testinput2 b/testdata/testinput2
index 0e24e78..b874f20 100644
--- a/testdata/testinput2
+++ b/testdata/testinput2
@@ -6055,4 +6055,8 @@ a)"xI
 
 /A +/extended
 
+/a\z/
+    a
+    a\=noteol 
+
 # End of testinput2
diff --git a/testdata/testoutput2 b/testdata/testoutput2
index 68800fb..c1bc0e6 100644
--- a/testdata/testoutput2
+++ b/testdata/testoutput2
@@ -17946,6 +17946,12 @@ No match
 
 /A +/extended
 
+/a\z/
+    a
+ 0: a
+    a\=noteol 
+ 0: a
+
 # End of testinput2
 Error -70: PCRE2_ERROR_BADDATA (unknown error number)
 Error -62: bad serialized data
-- 
2.33.0

