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
TreeSubSet.Min and TreeSubSet.Max work incorrectly with custom comparer #54834
Comments
Tagging subscribers to this area: @eiriktsarpalis Issue DetailsDescriptionThe code below gives the wrong result:
According to the documentation of IComparer.Compare() it can return any integer, not only [-1, 0, 1]. So this comparer should be ok. ConfigurationI run it on .net 5, but I think it is present in other versions as well. Regression?A similar issue was mentioned in 20474 (without comparer though). So I think it is regression. Other informationI have found the reason why it happens in code: runtime/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.TreeSubSet.cs Line 135 in 57bfe47
runtime/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.TreeSubSet.cs Line 164 in 57bfe47
The comparison result is checked against 1 and -1, instead of (comp > 0) and (comp < 0).
|
This seems like a bug introduced by dotnet/corefx#16771. It seems like a trivial fix, would you be willing to contribute a PR? |
I haven't contributed here yet, but yes, I can try. |
Thanks, assigning the issue to you. A couple of tests reproducing your scenario for both min and max should suffice. Please let me know if you have any questions on the dev workflow. |
Add test to check fix. Checking comparison result against 1 or -1 causes problems when custom comparer is used. As a result Min and Max properties return incorrect values.
Description
The code below gives the wrong result:
According to the documentation of IComparer.Compare() it can return any integer, not only [-1, 0, 1]. So this comparer should be ok.
Configuration
I run it on .net 5, but I think it is present in other versions as well.
Regression?
A similar issue was mentioned in 20474 (without comparer though). So I think it is regression.
Other information
I have found the reason why it happens in code:
runtime/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.TreeSubSet.cs
Line 135 in 57bfe47
runtime/src/libraries/System.Collections/src/System/Collections/Generic/SortedSet.TreeSubSet.cs
Line 164 in 57bfe47
The comparison result is checked against 1 and -1, instead of (comp > 0) and (comp < 0).
The text was updated successfully, but these errors were encountered: