Skip to content

Commit 4ded85c

Browse files
committed
Merge pull request #11 from baarde/fix-rdar-5177598
Fix a bug in CFReadStreamGetBuffer for data streams
2 parents a946561 + f47bb65 commit 4ded85c

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

CoreFoundation/Stream.subproj/CFConcreteStreams.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -592,21 +592,23 @@ static CFIndex dataRead(CFReadStreamRef stream, UInt8 *buffer, CFIndex bufferLen
592592

593593
static const UInt8 *dataGetBuffer(CFReadStreamRef stream, CFIndex maxBytesToRead, CFIndex *numBytesRead, CFStreamError *error, Boolean *atEOF, void *info) {
594594
_CFReadDataStreamContext *dataCtxt = (_CFReadDataStreamContext *)info;
595-
const UInt8 *bytes = CFDataGetBytePtr(dataCtxt->data);
596-
if (dataCtxt->loc - bytes > maxBytesToRead) {
595+
const UInt8 *bytePtr = CFDataGetBytePtr(dataCtxt->data);
596+
CFIndex length = CFDataGetLength(dataCtxt->data);
597+
CFIndex bytesToRead = bytePtr + length - dataCtxt->loc;
598+
if (bytesToRead > maxBytesToRead) {
597599
*numBytesRead = maxBytesToRead;
598600
*atEOF = FALSE;
599601
} else {
600-
*numBytesRead = dataCtxt->loc - bytes;
602+
*numBytesRead = bytesToRead;
601603
*atEOF = TRUE;
602604
}
603605
error->error = 0;
604-
bytes = dataCtxt->loc;
606+
const UInt8 *buffer = dataCtxt->loc;
605607
dataCtxt->loc += *numBytesRead;
606608
if (dataCtxt->scheduled && !*atEOF) {
607609
CFReadStreamSignalEvent(stream, kCFStreamEventHasBytesAvailable, NULL);
608610
}
609-
return bytes;
611+
return buffer;
610612
}
611613

612614
static Boolean dataCanRead(CFReadStreamRef stream, void *info) {

0 commit comments

Comments
 (0)