develooper Front page | perl.perl5.porters | Postings from July 2009

Regex inside regex

Thread Next
From:
Bram
Date:
July 25, 2009 08:14
Subject:
Regex inside regex
Message ID:
20090725171348.gplx80rfms4ckoo4@horde.wizbit.be
Currently calling a regex inside a regex results in memory corruption  
in the regex variable.

This can result in: segmentation faults, out of memory errors,  
incorrect values, ... meaning undefined/unexpected behaviour.

Examples (on my system, may or may not be reproducible everywhere):


#!/usr/bin/perl -l

print $];
*c = sub { "foo" =~ m/bar/ };
sub parse_re {
   $_[0] =~ m{(?{ c() })};
}
parse_re();
__END__

Running it with blead:
5.011000
Segmentation fault



#!/usr/bin/perl -l

*c = sub {
   "foo" =~ m/(f)(o)(o)/;
};

if ("abcdefghi" =~ m/(abc)(def)(?{ c() })(ghi)/) {
   print "match: $1, $2, $3 & $&";
}
else {
   print "no match";
}


*d = sub {
   "foo" =~ m/(b)(a)(r)/;
};

if ("abcdefghi" =~ m/(abc)(def)(?{ d() })(ghi)/) {
   print "match: $1, $2, $3 & $&";
}
else {
   print "no match";
}
__END__

Running it:
match: abc, def,  & abcdef
no match

In the first regex $3 is corrupted (empty instead of ghi);
In the second regex the match fails.


The question:

Is it possible to turn this into defined/expected behaviour?
For example by adding a panic/run time error/... when a regex is being  
started when a regex is already running? (read: what should be done to  
accomplish that?)


Best regards,

Bram



Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About