Skip to content

Fix CFCharacterSet equality detection #1308

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions CoreFoundation/String.subproj/CFCharacterSet.c
Original file line number Diff line number Diff line change
Expand Up @@ -896,12 +896,16 @@ static Boolean __CFCharacterSetEqual(CFTypeRef cf1, CFTypeRef cf2) {

case __kCFCharSetClassString:
if (isInvertStateIdentical) {
const CFIndex len1 = __CFCSetStringLength((CFCharacterSetRef)cf1);
if (len1 != __CFCSetStringLength((CFCharacterSetRef)cf2)) {
return false;
}
const UniChar *buf1 = __CFCSetStringBuffer((CFCharacterSetRef)cf1);
const UniChar *buf1End = buf1 + __CFCSetStringLength((CFCharacterSetRef)cf1);
const UniChar *buf1End = buf1 + len1;
const UniChar *buf2 = __CFCSetStringBuffer((CFCharacterSetRef)cf2);
const UniChar *buf2End = buf2 + __CFCSetStringLength((CFCharacterSetRef)cf2);
const UniChar *buf2End = buf2 + len1; // lengths are equal

while ((buf1 < buf1End) && (buf2 < buf2End)) {
while ((buf1 < buf1End) || (buf2 < buf2End)) {
UniChar char1 = *buf1;
UniChar char2 = *buf2;

Expand Down Expand Up @@ -1320,6 +1324,7 @@ Boolean _CFCharacterSetInitWithCharactersInRange(CFMutableCharacterSetRef cset,
if (!__CFCSetGenericInit(cset, __kCFCharSetClassRange)) return false;
__CFCSetPutRangeFirstChar(cset, theRange.location);
__CFCSetPutRangeLength(cset, theRange.length);
__CFCharacterSetHash(cset);
} else {
if (!__CFCSetGenericInit(cset, __kCFCharSetClassBitmap)) return false;
__CFCSetPutBitmapBits(cset, NULL);
Expand All @@ -1338,6 +1343,7 @@ CFCharacterSetRef CFCharacterSetCreateWithCharactersInRange(CFAllocatorRef alloc
if (!(cset = __CFCSetGenericCreate(allocator, __kCFCharSetClassRange))) return NULL;
__CFCSetPutRangeFirstChar(cset, theRange.location);
__CFCSetPutRangeLength(cset, theRange.length);
__CFCharacterSetHash(cset);
} else {
if (!(cset = __CFCSetGenericCreate(allocator, __kCFCharSetClassBitmap))) return NULL;
__CFCSetPutBitmapBits(cset, NULL);
Expand Down Expand Up @@ -1366,6 +1372,7 @@ Boolean _CFCharacterSetInitWithCharactersInString(CFMutableCharacterSetRef cset,
if (0 == length) {
__CFCSetPutHasHashValue(cset, true); // _hashValue is 0
} else if (length > 1) { // Check for surrogate
__CFCharacterSetHash(cset);
const UTF16Char *characters = __CFCSetStringBuffer(cset);
const UTF16Char *charactersLimit = characters + length;

Expand Down Expand Up @@ -1407,6 +1414,7 @@ CFCharacterSetRef CFCharacterSetCreateWithCharactersInString(CFAllocatorRef allo
if (0 == length) {
__CFCSetPutHasHashValue(cset, true); // _hashValue is 0
} else if (length > 1) { // Check for surrogate
__CFCharacterSetHash(cset);
const UTF16Char *characters = __CFCSetStringBuffer(cset);
const UTF16Char *charactersLimit = characters + length;

Expand Down Expand Up @@ -1507,6 +1515,7 @@ CF_INLINE Boolean __CFCharacterSetInitWithBitmapRepresentation(CFAllocatorRef al
}
}
}
__CFCharacterSetHash(cset);
} else {
__CFCSetPutBitmapBits(cset, NULL);
__CFCSetPutHasHashValue(cset, true); // Hash value is 0
Expand Down Expand Up @@ -1547,7 +1556,7 @@ CFCharacterSetRef CFCharacterSetCreateInvertedSet(CFAllocatorRef alloc, CFCharac
Boolean _CFCharacterSetInitMutable(CFMutableCharacterSetRef cset) {
if (!__CFCSetGenericInit(cset, __kCFCharSetClassBitmap| __kCFCharSetIsMutable)) return false;
__CFCSetPutBitmapBits(cset, NULL);
__CFCSetPutHasHashValue(cset, true);
__CFCSetPutHasHashValue(cset, true); // Hash value is 0
Copy link
Contributor

Choose a reason for hiding this comment

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

Was this comment copied and pasted from above? I thought the comment was regarding the condition rather than the action of the line, so I'm not sure it makes sense to add it here - but @phausler would know more, I suspect.

Copy link
Contributor Author

@sashabelonogov sashabelonogov Nov 10, 2017

Choose a reason for hiding this comment

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

I simply added it for the consistency, in all the similar places the same comment exist.

return true;
}
#endif
Expand Down
11 changes: 3 additions & 8 deletions TestFoundation/TestCharacterSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -330,16 +330,11 @@ class TestCharacterSet : XCTestCase {
("12345", "12345")
]

/*
Tests disabled due to CoreFoundation bug?
These NSCharacterSet pairs are (wrongly?) evaluated to be equal. Same behaviour can be observed on macOS 10.12.
Interestingly, on iOS 11 Simulator, they are evaluted to be _not_ equal,
while on iOS 10.3.1 Simulator, they are evaluted to be equal.
*/
let notEqualPairs = [
("abc", "123"),
// ("ab", "abc"),
// ("abc", "")
("ab", "abc"),
("abc", ""),
("abc", "abd")
]

for pair in equalPairs {
Expand Down