Subject: | testRegExpr can cause memory leak |
The sub testRegExpr in Template.pm occasionally causes a memory leak. This is caused by the usage of the g-flag. Below a revised version of this sub.
sub testRegExpr {
my $self = shift;
my $text = shift;
my $reg = shift;
my $insensitive = shift;
my $dynamic = shift;
if ($dynamic) {
$reg = $self->capturedParse($reg);
}
my $eng = $self->engine;
if ($reg =~ s/^\^//) {
unless ($eng->linestart) {
return '';
}
} elsif ($reg =~ s/^\\(b)//i) {
my $lastchar = $self->engine->lastchar;
if ($1 eq 'b') {
if ($lastchar =~ /\w/) { return '' }
} else {
if ($lastchar =~ /\W/) { return '' }
}
}
$reg = "^($reg)";
my $sample = $$text;
# emergency measurements to avoid exception (szabgab)
$reg = eval { qr/$reg/ };
if ($@) {
warn $@;
return '';
}
my $match;
if ($insensitive) {
if ($sample =~ /$reg/i) {
$match = $1;
if ($#-) {
no strict 'refs';
my @cap = map {$$_} 2 .. $#-;
$self->captured(\@cap)
}
}
} else {
if ($sample =~ /$reg/) {
$match = $1;
if ($#-) {
no strict 'refs';
my @cap = map {$$_} 2 .. $#-;
$self->captured(\@cap);
}
}
}
if ((defined($match)) and ($match ne '')) {
return $self->parseResult($text, $match, @_);
}
return ''
}