Skip to content

Conversation

GrabYourPitchforks
Copy link
Member

@GrabYourPitchforks GrabYourPitchforks commented Feb 7, 2020

Fixes #1060

This validates that if the target string in a call to string.Replace has zero collation weight, we'll throw ArgumentException (just as if the target string were empty) rather than enter an infinite loop.

I'm reusing the original exception text rather than creating a new resource string because I don't expect this to be a common scenario at all.

if (matchLength == 0)
{
throw new ArgumentException(SR.Argument_StringZeroLength, nameof(oldValue));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After seeing the customer scenario, it may be better to just return from the method without throwing. what you think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that, but then why not have string.Replace(string.Empty) also return the original string without throwing?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not have string.Replace(string.Empty) also return the original string without throwing?

I am ok to make this not throw too. Throwing will require the callers to handle the exceptions and I am not expecting the callers will have the knowledge we can throw in this case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also fine with making empty target strings not throw, as you had suggested. Would we be able to make this change or would it be considered too breaking?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would we be able to make this change or would it be considered too breaking?

I think we can document the breaking changes for this case. The breaking scenario will be very limited as we are going not to throw in cases we used to throw before.

@GrabYourPitchforks
Copy link
Member Author

After speaking offline with @tarekgh, seems like this might be the best course of action:

  1. Have this PR not throw when given a zero-weight target string. It instead just terminates the replacement loop and returns whatever work has been done so far. This should make it easier to port downlevel.
  2. If we care, send a separate PR to unify the "empty target string" and "zero-weight target string" behaviors so that neither throws. This would affect netcoreapp5.0+ only and would be to provide improved consistency for the API logic.

@GrabYourPitchforks
Copy link
Member Author

Looks like System.Threading.Tasks.Tests is failing across the board in CI right now. Unrelated to this PR, so going forward with merging.

@GrabYourPitchforks GrabYourPitchforks merged commit 50bd5ba into dotnet:master Feb 11, 2020
@GrabYourPitchforks GrabYourPitchforks deleted the string_replace_2 branch February 11, 2020 04:30
GrabYourPitchforks added a commit to GrabYourPitchforks/coreclr that referenced this pull request Feb 12, 2020
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.
GrabYourPitchforks added a commit to GrabYourPitchforks/coreclr that referenced this pull request Feb 12, 2020
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.
Anipik pushed a commit to dotnet/coreclr that referenced this pull request Feb 18, 2020
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.
jashook pushed a commit to dotnet/coreclr that referenced this pull request Apr 17, 2020
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.
@ghost ghost locked as resolved and limited conversation to collaborators Dec 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

A call to string.Replace does not terminate
4 participants