diff --git a/Source/CBLMultipartReader.m b/Source/CBLMultipartReader.m index 3e758b464..facad7a44 100644 --- a/Source/CBLMultipartReader.m +++ b/Source/CBLMultipartReader.m @@ -146,15 +146,17 @@ - (void) deleteUpThrough: (NSRange)r { [_buffer replaceBytesInRange: NSMakeRange(0, NSMaxRange(r)) withBytes: NULL length: 0]; } -- (void) trimBuffer { +- (BOOL) appendAndTrimBuffer { NSUInteger bufLen = _buffer.length; NSUInteger boundaryLen = _boundary.length; if (bufLen > boundaryLen) { // Leave enough bytes in _buffer that we can find an incomplete boundary string NSRange trim = NSMakeRange(0, bufLen - boundaryLen); - [_delegate appendToPart: [_buffer subdataWithRange: trim]]; + if (![_delegate appendToPart: [_buffer subdataWithRange: trim]]) + return NO; [self deleteUpThrough: trim]; } + return YES; } @@ -221,7 +223,10 @@ - (void) appendData: (NSData*)data { [self deleteUpThrough: r]; nextState = kInHeaders; } else { - [self trimBuffer]; + if (![self appendAndTrimBuffer]) { + [self stop]; + break; + } } break; }