Skip to content
This repository has been archived by the owner on Nov 1, 2021. It is now read-only.

Commit

Permalink
Fix buffer copy to prevent response pollution -- fixes #23, closes #24
Browse files Browse the repository at this point in the history
  • Loading branch information
goaway committed Jan 15, 2014
1 parent 964603e commit 951317a
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions SPDY/SPDYStream.m
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,11 @@ - (void)didReceiveResponse:(NSDictionary *)headers

- (void)didLoadData:(NSData *)data
{
if (_compressedResponse && data.length > 0) {
_zlibStream.avail_in = (uInt)data.length;
NSUInteger dataLength = data.length;
if (dataLength == 0) return;

if (_compressedResponse) {
_zlibStream.avail_in = (uInt)dataLength;
_zlibStream.next_in = (uint8_t *)data.bytes;

while (_zlibStreamStatus == Z_OK && (_zlibStream.avail_in > 0 || _zlibStream.avail_out == 0)) {
Expand All @@ -355,11 +358,12 @@ - (void)didLoadData:(NSData *)data
_zlibStreamStatus = inflate(&_zlibStream, Z_SYNC_FLUSH);

NSMutableData *inflatedData = [[NSMutableData alloc] initWithBytesNoCopy:inflatedBytes length:DECOMPRESSED_CHUNK_LENGTH freeWhenDone:YES];
inflatedData.length = DECOMPRESSED_CHUNK_LENGTH - _zlibStream.avail_out;
if (inflatedData.length > 0) {
NSUInteger inflatedLength = DECOMPRESSED_CHUNK_LENGTH - _zlibStream.avail_out;
inflatedData.length = inflatedLength;
if (inflatedLength > 0) {
[_client URLProtocol:_protocol didLoadData:inflatedData];
}

// This can happen if the decompressed data is size N * DECOMPRESSED_CHUNK_LENGTH,
// in which case we had to make an additional call to inflate() despite there being
// no more input to ensure there wasn't any pending output in the zlib stream.
Expand All @@ -377,9 +381,8 @@ - (void)didLoadData:(NSData *)data
[_client URLProtocol:_protocol didFailWithError:error];
}
} else {
if (data.length > 0) {
[_client URLProtocol:_protocol didLoadData:[data copy]];
}
NSData *dataCopy = [[NSData alloc] initWithBytes:data.bytes length:dataLength];
[_client URLProtocol:_protocol didLoadData:dataCopy];
}
}

Expand Down

0 comments on commit 951317a

Please sign in to comment.