diff options
author | Yves Orton <demerphq@gmail.com> | 2023-01-14 11:46:03 +0100 |
---|---|---|
committer | Yves Orton <demerphq@gmail.com> | 2023-01-15 17:20:48 +0100 |
commit | 37040543d024b3ecb0aecd78849bd5af61408d02 (patch) | |
tree | 971e23b6036477ccd688359e190b70f2858c6e31 /regexec.c | |
parent | 3645ca4ee1a59fae1a6d6817c4582968ffd0a731 (diff) | |
download | perl-37040543d024b3ecb0aecd78849bd5af61408d02.tar.gz |
regexec.c - fix memory leak in EVAL.
EVAL was calling regcppush twice per invocation, once before executing the
callback, and once after. But not regcppop'ing twice. So each time we
would accumulate an extra "frame" of data. This is/was hidden somewhat by
the way we eventually "blow" the stack, so the extra data was just thrown
away at the end.
This removes the second set of pushes so that the save stack stays a stable
size as it unwinds from each failed eval.
We also weren't cleaning up after a (?{...}) when we failed to match to its
right. This unwinds the stack and restores the parens properly.
This adds tests to check how the save stack grows during patterns using
(?{ ... }) and (??{ ... }) and ensure that when we backtrack and re-execute
the EVAL again it cleans up the stack as it goes.
Diffstat (limited to 'regexec.c')
-rw-r--r-- | regexec.c | 7 |
1 files changed, 5 insertions, 2 deletions
@@ -8072,7 +8072,7 @@ S_regmatch(pTHX_ regmatch_info *reginfo, char *startpos, regnode *prog) CV *newcv; /* save *all* paren positions */ - regcppush(rex, 0, maxopenparen); + ST.cp = regcppush(rex, 0, maxopenparen); REGCP_SET(ST.lastcp); if (!caller_cv) @@ -8349,9 +8349,11 @@ S_regmatch(pTHX_ regmatch_info *reginfo, char *startpos, regnode *prog) EVAL_CLOSE_PAREN_CLEAR(st); /* ST.close_paren = 0; * close_paren only for GOSUB */ ST.prev_recurse_locinput= NULL; /* only used for GOSUB */ - /* Save all the seen positions so far. */ + + /* note we saved the paren state earlier: ST.cp = regcppush(rex, 0, maxopenparen); REGCP_SET(ST.lastcp); + */ /* and set maxopenparen to 0, since we are starting a "fresh" match */ maxopenparen = 0; /* run the pattern returned from (??{...}) */ @@ -8441,6 +8443,7 @@ S_regmatch(pTHX_ regmatch_info *reginfo, char *startpos, regnode *prog) case EVAL_B_fail: /* unsuccessful B in (?{...})B */ REGCP_UNWIND(ST.lastcp); + regcppop(rex, &maxopenparen); sayNO; case EVAL_postponed_AB_fail: /* unsuccessfully ran A or B in (??{A})B */ |