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
Fix a bug in CFReadStreamGetBuffer for data streams #11
Conversation
Hi there, CFReadStreamGetBuffer hasn't worked properly forever (not quite relevant link: http://lists.apple.com/archives/macnetworkprog/2002/Jul/msg00046.html) and I fear that fixing it would encourage use of the CF and NS variants. "GetBuffer" is fundamentally incompatible with efforts to make CFStream (and NSStream) in any way thread safe. My preference would be to remove the API from the swift open source drop, and deprecate it from Foundation and CoreFoundation. Since CFReadStreamGetBuffer is akin to CFStringGetCStringPtr in that it can (and will) fail if it cannot satisfy its O(1) performance constraint , anyone trying to use it should already have fallback code in place. I was going to make comments on alternatives, but they should be pretty obvious. There's no public API I can think of that would consume an NS/CFInputStream and expect GetBuffer to work. |
@salgernon it sounds like maybe we should still take this though, because reading more bytes than you should could be a pretty big problem. Even if this fix is just for the Darwin CF. |
@parkera, In the public API, the only way to implement an NSInputStream is by subclasses; in that case, a subclassser CAN provide an implementation of -getBuffer:length that works. However, since the stream consumer must have code to deal with the case where the get buffer fails, I would suggest that we ALWAYS make it fail while in the CFStream implementation, and deprecating the method / function. I can't figure out when -getBuffer:length: is useful in any case where you didn't actually know that you had a memory stream in the first place, in which case, you may as well have been passed an [NSData subdataWithRange:]. (Oh, and while i"m on the subject, +[NSData dataWithBytesNoCopy:length:] should be deprecated in favor of a variant that takes an explicit destructor (or a block; there's a version of -initWithBytesNoCopy:length:deallocator:)) I guess my rant comes down to: if you've got a buffer of bytes, you don't need a stream unless you need to hand the stream to a subsystem. That subsystem cannot assume that any stream passed to it will be able to respond successfully to -getBuffer, so will always need to resort to -read:. Maybe we should take this to the API group? |
But if somebody wants to modify the |
Fix a bug in CFReadStreamGetBuffer for data streams
I'm going to take this one for the buffer fix, and we will discuss the deprecation in another thread. |
[swiftpm] Stop hard-coding the index store path
The
dataGetBuffer
function computes the number of bytes that have been read from the stream so far when it should compute the number of bytes remaining, with the following issues:AtEnd
.That bug has been affecting CoreFoundation and Foundation on OS X and iOS for a while.
That bug will eventually affect
NSInputStream
'sgetBuffer(:length:)
when it's implemented.Reference: http://openradar.me/5177598
Unit Test: baarde/swift-corelibs-foundation@81125ffc83051e2ebdca79cb504b51fd389dba5f