From b4b4098794a58914e4a1d2897c59d1fe995d1a0d Mon Sep 17 00:00:00 2001 From: Levi Broderick Date: Tue, 18 Feb 2020 13:32:57 -0800 Subject: 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. --- src/System.Private.CoreLib/shared/System/String.Manipulation.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/System.Private.CoreLib/shared/System/String.Manipulation.cs b/src/System.Private.CoreLib/shared/System/String.Manipulation.cs index 564fca0dcd..daf6fc3dff 100644 --- a/src/System.Private.CoreLib/shared/System/String.Manipulation.cs +++ b/src/System.Private.CoreLib/shared/System/String.Manipulation.cs @@ -1020,7 +1020,11 @@ namespace System do { index = ci.IndexOf(this, oldValue, startIndex, this.Length - startIndex, options, &matchLength); - if (index >= 0) + + // There's the possibility that 'oldValue' has zero collation weight (empty string equivalent). + // If this is the case, we behave as if there are no more substitutions to be made. + + if (index >= 0 && matchLength > 0) { // append the unmodified portion of string result.Append(this, startIndex, index - startIndex); -- cgit v1.2.3