Index: src/OFHTTPClient.m ================================================================== --- src/OFHTTPClient.m +++ src/OFHTTPClient.m @@ -71,11 +71,11 @@ @interface OFHTTPClientRequestBodyStream: OFStream { OFHTTPClientRequestHandler *_handler; OFTCPSocket *_socket; - intmax_t _contentLength, _written; + uintmax_t _contentLength, _written; bool _closed; } - (instancetype)initWithHandler: (OFHTTPClientRequestHandler *)handler socket: (OFTCPSocket *)sock; @@ -83,11 +83,11 @@ @interface OFHTTPClientResponse: OFHTTPResponse { OFTCPSocket *_socket; bool _hasContentLength, _chunked, _keepAlive, _atEndOfStream; - size_t _toRead; + uintmax_t _toRead; } @property (nonatomic, setter=of_setKeepAlive:) bool of_keepAlive; - (instancetype)initWithSocket: (OFTCPSocket *)sock; @@ -757,10 +757,11 @@ { self = [super init]; @try { OFDictionary OF_GENERIC(OFString *, OFString *) *headers; + intmax_t contentLength; OFString *contentLengthString; _handler = [handler retain]; _socket = [sock retain]; @@ -768,13 +769,15 @@ contentLengthString = [headers objectForKey: @"Content-Length"]; if (contentLengthString == nil) @throw [OFInvalidArgumentException exception]; - _contentLength = [contentLengthString decimalValue]; - if (_contentLength < 0) + contentLength = [contentLengthString decimalValue]; + if (contentLength < 0) @throw [OFOutOfRangeException exception]; + + _contentLength = contentLength; if ([headers objectForKey: @"Transfer-Encoding"] != nil) @throw [OFInvalidArgumentException exception]; } @catch (id e) { [self release]; @@ -797,28 +800,18 @@ - (size_t)lowlevelWriteBuffer: (const void *)buffer length: (size_t)length { size_t written; -#if SIZE_MAX >= INTMAX_MAX - if (length > INTMAX_MAX) - @throw [OFOutOfRangeException exception]; -#endif - - if (INTMAX_MAX - _written < (intmax_t)length || - _written + (intmax_t)length > _contentLength) + if (UINTMAX_MAX - _written < length || + _written + length > _contentLength) @throw [OFOutOfRangeException exception]; written = [_socket writeBuffer: buffer length: length]; -#if SIZE_MAX >= INTMAX_MAX - if (written > INTMAX_MAX) - @throw [OFOutOfRangeException exception]; -#endif - - if (INTMAX_MAX - _written < (intmax_t)written) + if (UINTMAX_MAX - _written < written) @throw [OFOutOfRangeException exception]; _written += written; return written; @@ -881,15 +874,11 @@ if (toRead < 0) @throw [OFInvalidServerReplyException exception]; - if (sizeof(intmax_t) > sizeof(size_t) && - toRead > (intmax_t)SIZE_MAX) - @throw [OFOutOfRangeException exception]; - - _toRead = (size_t)toRead; + _toRead = toRead; } @catch (OFInvalidFormatException *e) { @throw [OFInvalidServerReplyException exception]; } } } @@ -920,26 +909,28 @@ } return 0; } - if (_toRead < length) - ret = [_socket readIntoBuffer: buffer - length: _toRead]; - else - ret = [_socket readIntoBuffer: buffer - length: length]; + if (length > _toRead) + length = (size_t)_toRead; + + ret = [_socket readIntoBuffer: buffer + length: length]; + + if (ret > length) + @throw [OFOutOfRangeException exception]; _toRead -= ret; return ret; } /* Chunked */ if (_toRead > 0) { if (length > _toRead) - length = _toRead; + length = (size_t)_toRead; length = [_socket readIntoBuffer: buffer length: length]; _toRead -= length; @@ -965,16 +956,16 @@ if (range.location != OF_NOT_FOUND) line = [line substringWithRange: of_range(0, range.location)]; @try { - uintmax_t toRead = [line hexadecimalValue]; + intmax_t toRead = [line hexadecimalValue]; - if (toRead > SIZE_MAX) + if (toRead < 0) @throw [OFOutOfRangeException exception]; - _toRead = (size_t)toRead; + _toRead = toRead; } @catch (OFInvalidFormatException *e) { @throw [OFInvalidServerReplyException exception]; } if (_toRead == 0) {