Skip to content

Commit

Permalink
Fix startingStackpos tracking for loop in loop (#79382)
Browse files Browse the repository at this point in the history
* 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.

* Update src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexCompiler.cs
  • Loading branch information
stephentoub authored Dec 8, 2022
1 parent 1ca6a6f commit 41eab87
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4135,8 +4135,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 @@ -4147,8 +4149,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 @@ -4848,11 +4848,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 @@ -4868,9 +4872,15 @@ void EmitLoop(RegexNode node)
EmitTimeoutCheckIfNeeded();

// iterationCount = base.runstack[--runstack];
// startingStackpos = base.runstack[--runstack];
// 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 @@ -392,6 +392,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

0 comments on commit 41eab87

Please sign in to comment.