@@ -3514,7 +3514,14 @@ S_find_byclass(pTHX_ regexp * prog, const regnode *c, char *s,
35143514 return s ;
35153515}
35163516
3517- /* set RX_SAVED_COPY, RX_SUBBEG etc.
3517+ /* S_reg_set_capture_string():
3518+ * Save a pointer (RX_SUBBEG) to the just-matched string, so that $1 etc
3519+ * will have retrievable values. Typically it will make a copy rather than
3520+ * pointing direct (flags & REXEC_COPY_STR), so that subsequent
3521+ * modifications to the string won't affect the value of $1 etc. If so,
3522+ * the RXp_MATCH_COPIED flag will be set. It can sometimes avoid copying
3523+ * by using COW.
3524+ *
35183525 * flags have same meanings as with regexec_flags() */
35193526
35203527static void
@@ -11303,10 +11310,12 @@ S_setup_eval_state(pTHX_ regmatch_info *const reginfo)
1130311310 eval_state -> curpm = PL_curpm ;
1130411311 PL_curpm_under = PL_curpm ;
1130511312 PL_curpm = PL_reg_curpm ;
11313+ /* Temporarily set RXp_SUBBEG to the current string so that $1 etc
11314+ * are valid during code execution. If the current subbeg is a copy,
11315+ * then restore it at the end so that it gets properly freed when
11316+ * subbeg is finally updated after a successful match.
11317+ */
1130611318 if (RXp_MATCH_COPIED (rex )) {
11307- /* Here is a serious problem: we cannot rewrite subbeg,
11308- since it may be needed if this match fails. Thus
11309- $` inside (?{}) could fail... */
1131011319 eval_state -> subbeg = RXp_SUBBEG (rex );
1131111320 eval_state -> sublen = RXp_SUBLEN (rex );
1131211321 eval_state -> suboffset = RXp_SUBOFFSET (rex );
0 commit comments