Foundation: -isEqual: and -hash behaviour for NSCFBoolean incorrect.

Originator:jens.ayton
Number:rdar://4538282 Date Originated:05-May-2006 04:29 AM
Status:Closed Resolved:
Product:Mac OS X Product Version:
Classification:Serious Bug Reproducible:
 
Summary:
The documentation for NSObject states that: "If two objects are equal, they must have the same hash value." However, the Foundation classes NSCFNumber and NSCFBoolean do not honour this. Specifically, -isEqual: for these classes returns YES when comparing the integer NSNumber 1 with the boolean NSNumber true, but their hashes differ.

Steps to Reproduce:
Create an NSNumber object with the integer value 1. Create an NSNumber with the boolean value YES. Compare the two with isEqual:, and compare their hashes. (Code attached)

Expected Results:
There are two possible correct behaviours: either isEqual: should return YES and the hashes should be identical, or isEqual: should return NO and equality of hash values would not be an issue.

Actual Results:
isEqual: returns YES, but the hashes differ. Specifically, for the integer NSNumber, hash returns 1, while the hash for the boolean NSNumber is its address (inherited from the NSObject implementation).

Regression: 
None known.

Notes: 
Core Foundation’s equivalents handle this by returning FALSE from CFEqual(). This does not violate the definitions of CFEqual() and CFHash(), but is less convenient than defining the values to be equal. A better fix would be for the hash of NSCFBoolean to be 0 or 1 as appropriate.

Also note that this different behaviour means that -isEqual: and CFEqual() return different values for the same toll-free bridged core objects, which breaks expectations.

The fix that would have the least effect on existing software would be to change the hash of NSCFBoolean and continue to return different values for -isEqual: and CFEqual() despite the unexpectedness of this difference.

This bug was mentioned at <http://www.mulle-kybernetik.com/weblog/archives/000733.html>; the author does not specify whether he filed a bug and does not cover the CoreFoundation aspects.

Comments

Fixed in Snow Leopard.

By jens.ayton at Nov. 19, 2010, 12:03 p.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!