Skip to content
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

Mutable collections in foundation must not allow nil objects to be inserted #1607

Closed
sakrist opened this issue Dec 22, 2016 · 1 comment
Closed

Comments

@sakrist
Copy link
Contributor

sakrist commented Dec 22, 2016

I think it's critical issue.

Example of code:

NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
dictionary[@"key1"] = @"string";
dictionary[@"key2"] = nil;
dictionary[@"key3"] = NULL;
dictionary[@"key4"] = [NSNull null];

On iOS dictionary has only key1 and key4, print in debug:

{
    key1 = string;
    key5 = "<null>";
}

logged on WinObjC:

{
    key2 = (null)
    key3 = (null)
    key1 = string
    key5 = <null>
}

But need to remember that on iOS version of Foundation, next line cause exception:
[dictionary setObject:nil forKey:@"key5"];

Exception log:

2016-12-22 11:59:42.631 Test[44238:4517602] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** setObjectForKey: object cannot be nil (key: key5)'

But on WinObjC, it will let you insert nil object.

@rajsesh rajsesh added this to the 1701 milestone Jan 3, 2017
@rajsesh rajsesh changed the title Wrong behaviour on set nil/NULL for key into NSMutableDictionary Mutable collections in foundation must not allow nil objects to be inserted Jan 7, 2017
@rajsesh
Copy link
Contributor

rajsesh commented Jan 7, 2017

Note, this is also an issue in NSCFArray addObject, insertObject, (possibly also equivalent methods in NSCFAttributedString). Our assumption is that CF will handle null values. The api contract is that foundation APIs have to throw in these circumstances. Could we fix all these instances in one shot (see also #1435)

aballway added a commit that referenced this issue Jan 17, 2017
Now trying to insert into NSArray, NSDictionary, NSSet, and NSAttributedString will throw an NSException.

Fixes #1607

* Prevent insertion of nil into foundation collections

* Address CR feedback, throw for sets as well

* Missed one
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants