Skip to content

Commit

Permalink
Fix local initialization in generated regex conditional backreference…
Browse files Browse the repository at this point in the history
…s in loops (#73164)

The variable is later read if there's any backtracking and/or the conditional backreference is in a loop, but it was only being initialized if there was backtracking.
  • Loading branch information
stephentoub authored Aug 1, 2022
1 parent 0e2d041 commit 68bd58e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1799,7 +1799,7 @@ void EmitBackreferenceConditional(RegexNode node)
writer.WriteLine();
TransferSliceStaticPosToPos(); // make sure sliceStaticPos is 0 after each branch
string postYesDoneLabel = doneLabel;
if (postYesDoneLabel != originalDoneLabel)
if (postYesDoneLabel != originalDoneLabel || isInLoop)
{
writer.WriteLine($"{resumeAt} = 0;");
}
Expand All @@ -1821,7 +1821,7 @@ void EmitBackreferenceConditional(RegexNode node)
writer.WriteLine();
TransferSliceStaticPosToPos(); // make sure sliceStaticPos is 0 after each branch
postNoDoneLabel = doneLabel;
if (postNoDoneLabel != originalDoneLabel)
if (postNoDoneLabel != originalDoneLabel || isInLoop)
{
writer.WriteLine($"{resumeAt} = 1;");
}
Expand All @@ -1831,7 +1831,7 @@ void EmitBackreferenceConditional(RegexNode node)
// There's only a yes branch. If it's going to cause us to output a backtracking
// label but code may not end up taking the yes branch path, we need to emit a resumeAt
// that will cause the backtracking to immediately pass through this node.
if (postYesDoneLabel != originalDoneLabel)
if (postYesDoneLabel != originalDoneLabel || isInLoop)
{
writer.WriteLine($"{resumeAt} = 2;");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1884,7 +1884,7 @@ void EmitBackreferenceConditional(RegexNode node)
EmitNode(yesBranch);
TransferSliceStaticPosToPos();
Label postYesDoneLabel = doneLabel;
if (!isAtomic && postYesDoneLabel != originalDoneLabel)
if ((!isAtomic && postYesDoneLabel != originalDoneLabel) || isInLoop)
{
// resumeAt = 0;
Ldc(0);
Expand All @@ -1907,7 +1907,7 @@ void EmitBackreferenceConditional(RegexNode node)
EmitNode(noBranch);
TransferSliceStaticPosToPos(); // make sure sliceStaticPos is 0 after each branch
postNoDoneLabel = doneLabel;
if (!isAtomic && postNoDoneLabel != originalDoneLabel)
if ((!isAtomic && postNoDoneLabel != originalDoneLabel) || isInLoop)
{
// resumeAt = 1;
Ldc(1);
Expand All @@ -1919,7 +1919,7 @@ void EmitBackreferenceConditional(RegexNode node)
// There's only a yes branch. If it's going to cause us to output a backtracking
// label but code may not end up taking the yes branch path, we need to emit a resumeAt
// that will cause the backtracking to immediately pass through this node.
if (!isAtomic && postYesDoneLabel != originalDoneLabel)
if ((!isAtomic && postYesDoneLabel != originalDoneLabel) || isInLoop)
{
// resumeAt = 2;
Ldc(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@ public static IEnumerable<object[]> Match_MemberData()
yield return (@"(...)(?(1)\w*|\s*)[a1 ]", "---- ", RegexOptions.None, 0, 11, true, "--- ");
yield return (@"(...)(?(1)\w*|\s*)[a1 ]", "zabcaaaaaaa", RegexOptions.RightToLeft, 0, 11, true, "aaaa");
yield return (@"(...)(?(1)\w*|\s*)[a1 ]", "---- ", RegexOptions.RightToLeft, 0, 11, true, "--- ");
yield return (@"(aaa)(?(1)aaa|b?)*", "aaaaaa", RegexOptions.None, 0, 6, true, "aaaaaa");
}

// Character Class Subtraction
Expand Down

0 comments on commit 68bd58e

Please sign in to comment.