Skip to content

Commit

Permalink
Fix PCRE2_ENDANCHORED behaviour in recursion in pcre2_match(). Fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipHazel committed Nov 13, 2023
1 parent f5c4eb7 commit ce5b604
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 11 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ restrictions on POSIX classes.
pcre2_dfa_match() misbehaved. PCRE2_FIRSTLINE is now ignored for anchored
patterns.

38. If PCRE2_ENDANCHORED was set and the end of the pattern was reached during
a recursion, pcre2_match() misbehaved and gave the wrong match. For example,
the pattern /|a(?0)/ matched against "aaaa".


Version 10.42 11-December-2022
------------------------------
Expand Down
23 changes: 12 additions & 11 deletions src/pcre2_match.c
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,18 @@ fprintf(stderr, "++ op=%d\n", *Fecode);
case OP_ACCEPT:
case OP_END:

/* Fail if PCRE2_ENDANCHORED is set and the end of the match is not
the end of the subject. After (*ACCEPT) we fail the entire match (at this
position) but backtrack if we've reached the end of the pattern. This
applies whether or not we are in a recursion. */

if (Feptr < mb->end_subject &&
((mb->moptions | mb->poptions) & PCRE2_ENDANCHORED) != 0)
{
if (Fop == OP_END) RRETURN(MATCH_NOMATCH);
return MATCH_NOMATCH; /* (*ACCEPT) */
}

/* Handle end of a recursion. */

if (Fcurrent_recurse != RECURSE_UNSET)
Expand Down Expand Up @@ -871,17 +883,6 @@ fprintf(stderr, "++ op=%d\n", *Fecode);
Fstart_match == mb->start_subject + mb->start_offset)))
RRETURN(MATCH_NOMATCH);

/* Also fail if PCRE2_ENDANCHORED is set and the end of the match is not
the end of the subject. After (*ACCEPT) we fail the entire match (at this
position) but backtrack on reaching the end of the pattern. */

if (Feptr < mb->end_subject &&
((mb->moptions | mb->poptions) & PCRE2_ENDANCHORED) != 0)
{
if (Fop == OP_END) RRETURN(MATCH_NOMATCH);
return MATCH_NOMATCH;
}

/* We have a successful match of the whole pattern. Record the result and
then do a direct return from the function. If there is space in the offset
vector, set any pairs that follow the highest-numbered captured string but
Expand Down
3 changes: 3 additions & 0 deletions testdata/testinput2
Original file line number Diff line number Diff line change
Expand Up @@ -6041,4 +6041,7 @@ a)"xI
\x0a
abc\x0adef

/|a(?0)/endanchored
aaaa

# End of testinput2
3 changes: 3 additions & 0 deletions testdata/testinput6
Original file line number Diff line number Diff line change
Expand Up @@ -5039,4 +5039,7 @@
\x0a
abc\x0adef

/|a(?0)/endanchored
aaaa

# End of testinput6
4 changes: 4 additions & 0 deletions testdata/testoutput2
Original file line number Diff line number Diff line change
Expand Up @@ -17918,6 +17918,10 @@ No match
abc\x0adef
0: \x0a

/|a(?0)/endanchored
aaaa
0: aaaa

# End of testinput2
Error -70: PCRE2_ERROR_BADDATA (unknown error number)
Error -62: bad serialized data
Expand Down
8 changes: 8 additions & 0 deletions testdata/testoutput6
Original file line number Diff line number Diff line change
Expand Up @@ -7912,4 +7912,12 @@ Partial match:
abc\x0adef
0: \x0a

/|a(?0)/endanchored
aaaa
0: aaaa
1: aaa
2: aa
3: a
4:

# End of testinput6

0 comments on commit ce5b604

Please sign in to comment.