-
Notifications
You must be signed in to change notification settings - Fork 469
[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
Conversation
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!).
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). |
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. |
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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
[Data] fix crash when creating iterator of empty DispatchData Signed-off-by: Daniel A. Steffen <dsteffen@apple.com>
If you try to create an iterator from an empty DispatchData, it crashes due to force-unwrapping a nil pointer.