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

[SR-391] _CFSwiftDictionaryGetKeysAndValues() crashes with NULL arguments #4192

Closed
lhoward opened this issue Dec 26, 2015 · 6 comments
Closed
Assignees

Comments

@lhoward
Copy link
Contributor

lhoward commented Dec 26, 2015

Previous ID SR-391
Radar None
Original Reporter @lhoward
Type Bug
Status Closed
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug
Assignee @phausler
Priority Medium

md5: 9406824d62d27e835af51e42e1d7de5c

blocks:

  • SR-377 Implement NSKeyed[Un]Archiver

Issue Description:

It's valid to enumerate a dictionary with CFDictionaryGetKeysAndValues(), only retrieving the keys or values (or indeed nothing). See:

https://developer.apple.com/library/mac/documentation/CoreFoundation/Reference/CFDictionaryRef/#//apple_ref/c/func/CFDictionaryGetKeysAndValues

Thus, _CFSwiftDictionaryGetKeysAndValues needs to check keybuf and valuebuf are non-nil before assigning to them.

@phausler
Copy link
Member

Bridging a dictionary that contains NULL values is an error and cannot be properly represented by NSDictionary. The nullability markers on NSDictionary, NSArray, and NSSet are technically incorrect and should eventually be updated for the arrays of elements that can be null by declaration; since NSDictionary, NSArray and NSSet are not expected to be able to store nil.

@lhoward
Copy link
Contributor Author

lhoward commented Dec 28, 2015

It's not about NULL values, it's about the top-level keybuf/valbuf pointer to _CFSwiftDictionaryGetKeysAndValues().

@phausler
Copy link
Member

ah ok, yes that is correct; but the signature of UnsafeMutablePointer<Unmanaged<AnyObject>?> does not need to change for that since UnsafeMutablePointer can be compared to nil via NilLiteralConvertible, this should be a relatively easy fix

@lhoward
Copy link
Contributor Author

lhoward commented Dec 29, 2015

Right, this works for me:

internal func _CFSwiftDictionaryGetKeysAndValues(dictionary: AnyObject, keybuf: UnsafeMutablePointer<Unmanaged<AnyObject>?>, valuebuf: UnsafeMutablePointer<Unmanaged<AnyObject>?>) {
    var idx = 0
    (dictionary as! NSDictionary).enumerateKeysAndObjectsUsingBlock { key, value, _ in
        if keybuf != nil {
            keybuf[idx] = Unmanaged<AnyObject>.passUnretained(key)
        }
        if valuebuf != nil {
            valuebuf[idx] = Unmanaged<AnyObject>.passUnretained(value)
        }
        idx += 1
    }
}

@lhoward
Copy link
Contributor Author

lhoward commented Dec 29, 2015

lhoward@6724634

@lhoward
Copy link
Contributor Author

lhoward commented Dec 31, 2015

integrated in 0b43114

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
This issue was closed.
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

2 participants