@@ -3514,7 +3514,14 @@ S_find_byclass(pTHX_ regexp * prog, const regnode *c, char *s,
3514
3514
return s ;
3515
3515
}
3516
3516
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
+ *
3518
3525
* flags have same meanings as with regexec_flags() */
3519
3526
3520
3527
static void
@@ -11303,10 +11310,12 @@ S_setup_eval_state(pTHX_ regmatch_info *const reginfo)
11303
11310
eval_state -> curpm = PL_curpm ;
11304
11311
PL_curpm_under = PL_curpm ;
11305
11312
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
+ */
11306
11318
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... */
11310
11319
eval_state -> subbeg = RXp_SUBBEG (rex );
11311
11320
eval_state -> sublen = RXp_SUBLEN (rex );
11312
11321
eval_state -> suboffset = RXp_SUBOFFSET (rex );
0 commit comments