Skip to content

[Data] fix crash when creating iterator of empty DispatchData #111

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

Merged
merged 1 commit into from
Jul 20, 2016

Conversation

karwa
Copy link

@karwa karwa commented Jul 20, 2016

If you try to create an iterator from an empty DispatchData, it crashes due to force-unwrapping a nil pointer.

@dgrove-oss
Copy link
Contributor

dgrove-oss commented Jul 20, 2016

Thanks for the bug report and suggested fix. I'll defer to @mwwa on how he wants to actually fix it. Personally, I found the use of flatMap a little indirect. The snippet below worked for me (along with the change to the type of _ptr to UnsafePointer!).

/// Create an iterator over the given DisaptchData
    public init(_data: DispatchData) {
        var ptr: UnsafePointer<Void>?
        self._count = 0
        self._data = CDispatch.dispatch_data_create_map(_data.__wrapped, &ptr, &self._count)
        self._ptr = UnsafePointer(ptr)
        self._position = _data.startIndex
        assert(self._ptr != nil || self._count == self._position)
    }

I'd probably also leave out the assert in next() as the invariant it is testing is established in init.

Another thing I noticed when poking at this code is that the next() method doesn't enforce the stated precondition that "No preceding call to next() has returned nil". One can happily keep calling next() after it hits the end of the data and it will keep happily returning nil to you (because _position will still equal _count).

@karwa
Copy link
Author

karwa commented Jul 20, 2016

Sure, I suppose all we really need is to not force-unwrap. I just added extra safety because I don't believe we have Swift unit tests yet for dispatch. I'll update the code.

That documentation must have been written prior to changing the post-nil guarantees (https://github.com/apple/swift-evolution/blob/master/proposals/0052-iterator-post-nil-guarantee.md). I'll update that, too.

@mwwa
Copy link
Contributor

mwwa commented Jul 20, 2016

I'll happy with @dgrove-oss's implementation. I'll go ahead and make an equivalent change to the OS X overlay, too.

@@ -238,14 +238,15 @@ public struct DispatchDataIterator : IteratorProtocol, Sequence {
var ptr: UnsafePointer<Void>?
self._count = 0
self._data = CDispatch.dispatch_data_create_map(_data.__wrapped, &ptr, &self._count)
self._ptr = UnsafePointer(ptr!)
self._ptr = UnsafePointer(ptr)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this not also be accompanied by the type change on line 258?

Copy link
Author

Choose a reason for hiding this comment

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

Indeed, it got lost. Back now.

@mwwa mwwa merged commit ae71a91 into swiftlang:master Jul 20, 2016
das pushed a commit that referenced this pull request Aug 11, 2016
[Data] fix crash when creating iterator of empty DispatchData

Signed-off-by: Daniel A. Steffen <dsteffen@apple.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants