Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Fix startingStackpos tracking for loop in loop
This extends the fix made earlier around proper handling of the backtracking stack in EmitLoop.  If this loop is inside of another loop, then we need to preserve the startingStackpos on the backtracking stack upon successfully completing the loop, as the outer loop might iterate and cause another occurrence of this loop to run, which will then overwrite our single startingStackpos local.  If that iteration backtracks, we then need to be able to pop the previous iterations startingStackpos and restore its value.
  • Loading branch information
stephentoub committed Dec 8, 2022
commit d27c95a441306dd3b3cb8b6a158e8554010648aa
Original file line number Diff line number Diff line change
Expand Up @@ -4146,8 +4146,10 @@ void EmitLoop(RegexNode node)
writer.WriteLine();

// Store the loop's state
EmitStackPush(iterationMayBeEmpty ?
new[] { startingPos!, iterationCount } :
EmitStackPush(
startingPos is not null && startingStackpos is not null ? new[] { startingPos, startingStackpos, iterationCount } :
startingPos is not null ? new[] { startingPos, iterationCount } :
startingStackpos is not null ? new[] { startingStackpos, iterationCount } :
new[] { iterationCount });

// Skip past the backtracking section
Expand All @@ -4158,8 +4160,10 @@ void EmitLoop(RegexNode node)
// Emit a backtracking section that restores the loop's state and then jumps to the previous done label
string backtrack = ReserveName("LoopBacktrack");
MarkLabel(backtrack, emitSemicolon: false);
EmitStackPop(iterationMayBeEmpty ?
new[] { iterationCount, startingPos! } :
EmitStackPop(
startingPos is not null && startingStackpos is not null ? new[] { iterationCount, startingStackpos, startingPos } :
startingPos is not null ? new[] { iterationCount, startingPos } :
startingStackpos is not null ? new[] { iterationCount, startingStackpos } :
new[] { iterationCount });

// We're backtracking. Check the timeout.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4918,11 +4918,15 @@ void EmitLoop(RegexNode node)
if (analysis.IsInLoop(node))
{
// Store the loop's state
EmitStackResizeIfNeeded(1 + (startingPos is not null ? 1 : 0));
EmitStackResizeIfNeeded(1 + (startingPos is not null ? 1 : 0) + (startingStackpos is not null ? 1 : 0));
if (startingPos is not null)
{
EmitStackPush(() => Ldloc(startingPos));
}
if (startingStackpos is not null)
{
EmitStackPush(() => Ldloc(startingStackpos));
}
EmitStackPush(() => Ldloc(iterationCount));

// Skip past the backtracking section
Expand All @@ -4941,6 +4945,11 @@ void EmitLoop(RegexNode node)
// startingPos = base.runstack[--runstack];
EmitStackPop();
Stloc(iterationCount);
if (startingStackpos is not null)
{
EmitStackPop();
Stloc(startingStackpos);
}
if (startingPos is not null)
{
EmitStackPop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,10 @@ public static IEnumerable<object[]> Match_MemberData()
yield return (@"(ver\.? |[_ ]+)?\d+(\.\d+){2,3}$", " Ver 2.0", RegexOptions.IgnoreCase, 0, 8, false, "");
yield return (@"(?:|a)?(?:\b\d){2,}", " a 0", RegexOptions.None, 0, 4, false, "");
yield return (@"(?:|a)?(\d){2,}", " a00a", RegexOptions.None, 0, 5, true, "a00");
yield return (@"^( | )?((\w\d){3,}){3,}", " 12345678901234567", RegexOptions.None, 0, 18, false, "");
yield return (@"^( | )?((\w\d){3,}){3,}", " 123456789012345678", RegexOptions.None, 0, 19, true, " 123456789012345678");
yield return (@"^( | )?((\w\d){3,}){3,}", " 123456789012345678", RegexOptions.None, 0, 20, true, " 123456789012345678");
yield return (@"^( | )?((\w\d){3,}){3,}", " 123456789012345678", RegexOptions.None, 0, 21, false, "");

// Using beginning/end of string chars \A, \Z: Actual - "\\Aaaa\\w+zzz\\Z"
yield return (@"\Aaaa\w+zzz\Z", "aaaasdfajsdlfjzzz", RegexOptions.IgnoreCase, 0, 17, true, "aaaasdfajsdlfjzzz");
Expand Down