Skip to content

Commit

Permalink
Make || alternations respect :ratchet mode
Browse files Browse the repository at this point in the history
:ratchet mode already stopped the regex engine from backtracking
into quantifiers and `|` (LTM) alternations.

Now it also stops it from backtracking into `||` (sequential)
alternations.

(Fixes RT #130117)
  • Loading branch information
smls committed Aug 27, 2017
1 parent e5e5a3f commit 263257a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/vm/jvm/QAST/Compiler.nqp
Expand Up @@ -5264,6 +5264,12 @@ class QAST::CompilerJAST {
my $endlabel := JAST::Label.new( :name($prefix ~ 'end') );
my $altlabel := JAST::Label.new( :name($prefix ~ $altcount) );
my $ajast := self.regex_jast(nqp::shift($iter));

my $mark_endlabel := &*REGISTER_MARK($endlabel);
self.regex_mark($il, $mark_endlabel,
$IVAL_MINUSONE,
$IVAL_ZERO);

while $iter {
$il.append($altlabel);
$altcount++;
Expand All @@ -5273,6 +5279,7 @@ class QAST::CompilerJAST {
JAST::Instruction.new( :op('lload'), %*REG<pos> ),
$IVAL_ZERO);
$il.append($ajast);
self.regex_commit($il, $mark_endlabel) if $node.backtrack eq 'r';
$il.append(JAST::Instruction.new( :op('goto'), $endlabel ));
$ajast := self.regex_jast(nqp::shift($iter));
}
Expand Down
2 changes: 2 additions & 0 deletions src/vm/moar/QAST/QASTRegexCompilerMAST.nqp
Expand Up @@ -316,13 +316,15 @@ class QAST::MASTRegexCompiler {
my $altlabel_index := self.rxjump();
my $altlabel := @!rxjumps[$altlabel_index];
my @amast := self.regex_mast(nqp::shift($iter));
self.regex_mark(@ins, $endlabel_index, %!reg<negone>, %!reg<zero>);
while $iter {
nqp::push(@ins, $altlabel);
$altcount++;
$altlabel_index := self.rxjump();
$altlabel := @!rxjumps[$altlabel_index];
self.regex_mark(@ins, $altlabel_index, %!reg<pos>, %!reg<zero>);
merge_ins(@ins, @amast);
self.regex_commit(@ins, $endlabel_index) if $node.backtrack eq 'r';
nqp::push(@ins, op('goto', $endlabel));
@amast := self.regex_mast(nqp::shift($iter));
}
Expand Down

0 comments on commit 263257a

Please sign in to comment.