From 37040543d024b3ecb0aecd78849bd5af61408d02 Mon Sep 17 00:00:00 2001 From: Yves Orton Date: Sat, 14 Jan 2023 11:46:03 +0100 Subject: 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. --- ext/XS-APItest/APItest.xs | 9 +++++++++ ext/XS-APItest/t/savestack.t | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 ext/XS-APItest/t/savestack.t (limited to 'ext') diff --git a/ext/XS-APItest/APItest.xs b/ext/XS-APItest/APItest.xs index ff7667b24a..a7f1d5f011 100644 --- a/ext/XS-APItest/APItest.xs +++ b/ext/XS-APItest/APItest.xs @@ -7931,3 +7931,12 @@ newSvNV(const char * string) RETVAL = SvNV(newSVpv(string, 0)); OUTPUT: RETVAL + +MODULE = XS::APItest PACKAGE = XS::APItest::savestack + +IV +get_savestack_ix() + CODE: + RETVAL = PL_savestack_ix; + OUTPUT: + RETVAL diff --git a/ext/XS-APItest/t/savestack.t b/ext/XS-APItest/t/savestack.t new file mode 100644 index 0000000000..0e7d628e37 --- /dev/null +++ b/ext/XS-APItest/t/savestack.t @@ -0,0 +1,37 @@ +#!perl -w + +use strict; +use warnings; +use Test::More; + +use XS::APItest; + +my %ix; +sub showix { + diag join ", ", map { $ix{$_} > 1 ? "$_ x $ix{$_}" : $_ } sort { $a <=> $b } keys %ix; +} +my $len = 100; +my $str= "a" x $len; +my $pat= join "|", map { "a" x $_ } 1 .. $len; + +$str=~/^($pat)(??{ $ix{get_savestack_ix()}++; "(?!)" })/; +my $keys= 0+keys %ix; +cmp_ok($keys,">",0, "We expect at least one key in %ix for (??{ ... }) test"); +cmp_ok($keys,"<=", 2, "We expect no more than two keys in %ix if (??{ ... }) does not leak") + or showix(); + +%ix= (); +$str=~/^($pat)(?{ $ix{my $x=get_savestack_ix()}++; })(?!)/; +$keys= 0+keys %ix; +cmp_ok($keys,">",0, "We expect at least one key in %ix for (?{ ... }) test"); +cmp_ok($keys, "<=", 2, "We expect no more than two keys in %ix if (?{ ... }) does not leak") + or showix(); + +%ix= (); +$str=~/^($pat)(?(?{ $ix{my $x=get_savestack_ix()}++; })x|y)(?!)/; +$keys= 0+keys %ix; +cmp_ok($keys,">",0, "We expect at least one key in %ix for (?(?{ ... })yes|no) test"); +cmp_ok($keys, "<=", 2, "We expect no more than two keys in %ix if (?(?{ ... })yes|no) does not leak") + or showix(); + +done_testing(); -- cgit v1.2.1