Skip to content

Commit

Permalink
Prevent IndexOutOfRangeException in RegexInterpreter (#97916)
Browse files Browse the repository at this point in the history
* Prevent IndexOutOfRangeException in RegexInterpreter

This update fixes the IndexOutOfRangeException in RegexInterpreter by enhancing the `TrackPush` and `TrackPush2` methods. The adjustment involves checking the runtrack position before decrementing it, ensuring that it doesn't become negative, which was the root cause of the exception. This prevents potential out-of-range errors when handling large numbers of repetitions in regular expressions.

Fix #62094

* Changed to call EnsureStorage() unconditionally.

If EnsureStorage() is called unconditionally, the array will be expanded, so the position will never become negative. When the conditions inside EnsureStorage() are true, it might be necessary to expand the array, regardless of the comparison between newpos and codepos.

https://github.com/dotnet/runtime/blob/6ebc8bd86dbc780b2a2a7daf3ab6020f9104f09e/src/libraries/System.Text.RegularExpressions/tests/FunctionalTests/Regex.MultipleMatches.Tests.cs#L461-L469

Before the change, in this test case, EnsureStorage() is not called because newpos == codepos == 6 from the first time until an exception occurs.

Fix #62049
  • Loading branch information
rhirano0715 authored Apr 13, 2024
1 parent 1e05afc commit 7c365ec
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ private void Advance(int i)
private void Goto(int newpos)
{
// When branching backward, ensure storage.
if (newpos < _codepos)
{
EnsureStorage();
}
EnsureStorage();

_codepos = newpos;
SetOperator((RegexOpcode)_code.Codes[newpos]);
Expand Down Expand Up @@ -144,10 +141,7 @@ private void Backtrack()
SetOperator((RegexOpcode)(_code.Codes[newpos] | back));

// When branching backward, ensure storage.
if (newpos < _codepos)
{
EnsureStorage();
}
EnsureStorage();

_codepos = newpos;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,19 +454,15 @@ public static IEnumerable<object[]> Matches_TestData()
};
}

if (engine != RegexEngine.Interpreter && // these tests currently fail with RegexInterpreter
RuntimeFeature.IsDynamicCodeCompiled) // if dynamic code isn't compiled, RegexOptions.Compiled falls back to the interpreter, for which these tests currently fail
// Fails on .NET Framework: [ActiveIssue("https://github.com/dotnet/runtime/issues/62094")]
yield return new object[]
{
// Fails on interpreter and .NET Framework: [ActiveIssue("https://github.com/dotnet/runtime/issues/62094")]
yield return new object[]
engine, @"(?:){93}", "x", RegexOptions.None, new[]
{
engine, @"(?:){93}", "x", RegexOptions.None, new[]
{
new CaptureData("", 0, 0),
new CaptureData("", 1, 0)
}
};
}
new CaptureData("", 0, 0),
new CaptureData("", 1, 0)
}
};
#endif
}
}
Expand Down

0 comments on commit 7c365ec

Please sign in to comment.