Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit b4b4098

Browse files
Port dotnet/runtime#31946 to release/3.1 branch (#28014)
When string.Replace is given a target string with zero collation weight, it would enter an infinite loop. It is now changed so that the call to Replace terminates when such a condition is encountered.
1 parent a95569a commit b4b4098

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/System.Private.CoreLib/shared/System/String.Manipulation.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1020,7 +1020,11 @@ private unsafe string ReplaceCore(string oldValue, string? newValue, CultureInfo
10201020
do
10211021
{
10221022
index = ci.IndexOf(this, oldValue, startIndex, this.Length - startIndex, options, &matchLength);
1023-
if (index >= 0)
1023+
1024+
// There's the possibility that 'oldValue' has zero collation weight (empty string equivalent).
1025+
// If this is the case, we behave as if there are no more substitutions to be made.
1026+
1027+
if (index >= 0 && matchLength > 0)
10241028
{
10251029
// append the unmodified portion of string
10261030
result.Append(this, startIndex, index - startIndex);

0 commit comments

Comments
 (0)