You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Equals and GetHashCode mismatch with StringComparer.InvariantCulture #1062
usingSystem;staticclassProgram{staticvoidMain(){// U+FFFD is REPLACEMENT CHARACTERConsole.WriteLine(StringComparer.InvariantCulture.Equals("","\uFFFD"));Console.WriteLine(StringComparer.InvariantCulture.GetHashCode("")==StringComparer.InvariantCulture.GetHashCode("\uFFFD"));}}
ACTUAL:
True
False
EXPECTED:
The second Boolean value is True if the first one is True.
OS ver is Microsoft Windows [Version 10.0.17763.914]
Dug into this a bit further, and seems like the NLS functions LCMapStringEx and CompareStringEx are being consistent with respect to each other, though I find it odd that they're ignoring the presence of the U+FFFD character. So that points to the issue being on the managed side, not the NLS side.
Culprit appears to be the quick-exit check at the beginning of the below method.
If there are indeed some non-empty strings that would compare as equivalent to the empty string, then we can't perform this shortcut and instead we need to call LCMapStringEx even on the empty string. Since the cchSrc parameter of LCMapStringEx does not allow 0, we can instead pass for lpSrcStr a pointer to a null-terminated zero-length string and pass for cchSrc the constant value -1. In the sample C++ application I wrote this appears to have the correct behavior.
Yes, there are other strings considered equal to "" by StringComparer.InvariantCulture, e.g, "\u0640" or "\0" (I am not evaluating whether it is reasonable or not). The shortcut is wrong then.
Activity
GrabYourPitchforks commentedon Dec 19, 2019
This and #1060 are likely NLS bugs. I'll follow up with the Windows localization team offline.
GrabYourPitchforks commentedon Dec 19, 2019
Dug into this a bit further, and seems like the NLS functions
LCMapStringExandCompareStringExare being consistent with respect to each other, though I find it odd that they're ignoring the presence of the U+FFFD character. So that points to the issue being on the managed side, not the NLS side.Culprit appears to be the quick-exit check at the beginning of the below method.
runtime/src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.Windows.cs
Lines 127 to 135 in 0a29c61
If there are indeed some non-empty strings that would compare as equivalent to the empty string, then we can't perform this shortcut and instead we need to call
LCMapStringExeven on the empty string. Since the cchSrc parameter ofLCMapStringExdoes not allow 0, we can instead pass for lpSrcStr a pointer to a null-terminated zero-length string and pass for cchSrc the constant value -1. In the sample C++ application I wrote this appears to have the correct behavior.VladimirReshetnikov commentedon Dec 19, 2019
Yes, there are other strings considered equal to
""byStringComparer.InvariantCulture, e.g,"\u0640"or"\0"(I am not evaluating whether it is reasonable or not). The shortcut is wrong then.