Skip to content

Commit

Permalink
Changed to call EnsureStorage() unconditionally.
Browse files Browse the repository at this point in the history
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 dotnet#62049
  • Loading branch information
rhirano0715 committed Mar 9, 2024
1 parent 9c3cceb commit 09e507c
Showing 1 changed file with 7 additions and 27 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 All @@ -74,15 +71,8 @@ private void TrackPush(int i1)
int[] localruntrack = runtrack!;
int localruntrackpos = runtrackpos;

if (localruntrackpos > 1)
{
localruntrack[--localruntrackpos] = i1;
localruntrack[--localruntrackpos] = _codepos;
}
else
{
localruntrackpos = 0;
}
localruntrack[--localruntrackpos] = i1;
localruntrack[--localruntrackpos] = _codepos;

runtrackpos = localruntrackpos;
}
Expand Down Expand Up @@ -128,16 +118,9 @@ private void TrackPush2(int i1, int i2)
int[] localruntrack = runtrack!;
int localruntrackpos = runtrackpos;

if (localruntrackpos > 2)
{
localruntrack[--localruntrackpos] = i1;
localruntrack[--localruntrackpos] = i2;
localruntrack[--localruntrackpos] = -_codepos;
}
else
{
localruntrackpos = 0;
}
localruntrack[--localruntrackpos] = i1;
localruntrack[--localruntrackpos] = i2;
localruntrack[--localruntrackpos] = -_codepos;

runtrackpos = localruntrackpos;
}
Expand All @@ -158,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

0 comments on commit 09e507c

Please sign in to comment.