-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Disallow string.Replace with a zero-weight target string #31946
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Disallow string.Replace with a zero-weight target string #31946
Conversation
if (matchLength == 0) | ||
{ | ||
throw new ArgumentException(SR.Argument_StringZeroLength, nameof(oldValue)); | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
After speaking offline with @tarekgh, seems like this might be the best course of action:
|
Looks like System.Threading.Tasks.Tests is failing across the board in CI right now. Unrelated to this PR, so going forward with merging. |
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.
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.
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.
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.
Fixes #1060
This validates that if the target string in a call to
string.Replace
has zero collation weight, we'll throwArgumentException
(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.