@@ -103,17 +103,17 @@ OF_DIRECT_MEMBERS @interface OFHTTPServerRequestBodyStream: OFStream { OFStreamSocket *_socket; bool _chunked; - intmax_t _toRead; + long long _toRead; bool _atEndOfStream, _setAtEndOfStream; } - (instancetype)initWithSocket: (OFStreamSocket *)sock chunked: (bool)chunked - contentLength: (uintmax_t)contentLength; + contentLength: (unsigned long long)contentLength; @end #ifdef OF_HAVE_THREADS OF_DIRECT_MEMBERS @interface OFHTTPServerThread: OFThread @@ -413,25 +413,22 @@ if (line.length == 0) { bool chunked = [[_headers objectForKey: @"Transfer-Encoding"] isEqual: @"chunked"]; OFString *contentLengthString = [_headers objectForKey: @"Content-Length"]; - intmax_t contentLength = 0; + unsigned long long contentLength = 0; if (contentLengthString != nil) { if (chunked || contentLengthString.length == 0) return [self sendErrorAndClose: 400]; @try { contentLength = - contentLengthString.decimalValue; + contentLengthString.unsignedLongLongValue; } @catch (OFInvalidFormatException *e) { return [self sendErrorAndClose: 400]; } - - if (contentLength < 0) - return [self sendErrorAndClose: 400]; } if (chunked || contentLengthString != nil) { [_requestBody release]; _requestBody = nil; @@ -480,12 +477,13 @@ of_range(0, pos)] retain]; @try { of_range_t range = of_range(pos + 1, value.length - pos - 1); - intmax_t portTmp = [value - substringWithRange: range].decimalValue; + unsigned long long portTmp = + [value substringWithRange: range] + .unsignedLongLongValue; if (portTmp < 1 || portTmp > UINT16_MAX) return [self sendErrorAndClose: 400]; _port = (uint16_t)portTmp; @@ -543,11 +541,11 @@ URL = [OFMutableURL URL]; URL.scheme = @"http"; URL.host = _host; if (_port != 80) - URL.port = [OFNumber numberWithUInt16: _port]; + URL.port = [OFNumber numberWithUnsignedShort: _port]; if ((pos = [_path rangeOfString: @"?"].location) != OF_NOT_FOUND) { OFString *path, *query; path = [_path substringWithRange: of_range(0, pos)]; @@ -583,18 +581,21 @@ @end @implementation OFHTTPServerRequestBodyStream - (instancetype)initWithSocket: (OFStreamSocket *)sock chunked: (bool)chunked - contentLength: (uintmax_t)contentLength + contentLength: (unsigned long long)contentLength { self = [super init]; @try { + if (contentLength > LLONG_MAX) + @throw [OFOutOfRangeException exception]; + _socket = [sock retain]; _chunked = chunked; - _toRead = contentLength; + _toRead = (long long)contentLength; if (_chunked && _toRead > 0) @throw [OFInvalidArgumentException exception]; } @catch (id e) { [self release]; @@ -631,11 +632,11 @@ /* Content-Length */ if (!_chunked) { size_t ret; - if (length > (uintmax_t)_toRead) + if (length > (unsigned long long)_toRead) length = (size_t)_toRead; ret = [_socket readIntoBuffer: buffer length: length]; @@ -680,11 +681,11 @@ if (_setAtEndOfStream && _toRead == 0) _atEndOfStream = true; return 0; } else if (_toRead > 0) { - if (length > (uintmax_t)_toRead) + if (length > (unsigned long long)_toRead) length = (size_t)_toRead; length = [_socket readIntoBuffer: buffer length: length]; @@ -696,10 +697,11 @@ return length; } else { void *pool = objc_autoreleasePoolPush(); OFString *line; of_range_t range; + unsigned long long toRead; @try { line = [_socket tryReadLine]; } @catch (OFInvalidEncodingException *e) { @throw [OFInvalidFormatException exception]; @@ -723,12 +725,14 @@ @throw [OFTruncatedDataException exception]; else @throw [OFInvalidFormatException exception]; } - if ((_toRead = line.hexadecimalValue) < 0) + toRead = [line unsignedLongLongValueWithBase: 16]; + if (toRead > LLONG_MAX) @throw [OFOutOfRangeException exception]; + _toRead = (long long)toRead; if (_toRead == 0) { _setAtEndOfStream = true; _toRead = -2; }