@@ -18,10 +18,11 @@ #import "OFMessagePackExtension.h" #import "OFData.h" #import "OFString.h" #import "OFInvalidArgumentException.h" +#import "OFOutOfRangeException.h" @implementation OFMessagePackExtension @synthesize type = _type, data = _data; + (instancetype)extensionWithType: (int8_t)type data: (OFData *)data @@ -98,11 +99,11 @@ prefix = 0xD8; [ret addItem: &prefix]; [ret addItem: &_type]; - } else if (count < 0x100) { + } else if (count <= UINT8_MAX) { uint8_t length; ret = [OFMutableData dataWithCapacity: count + 3]; prefix = 0xC7; @@ -110,11 +111,11 @@ length = (uint8_t)count; [ret addItem: &length]; [ret addItem: &_type]; - } else if (count < 0x10000) { + } else if (count <= UINT16_MAX) { uint16_t length; ret = [OFMutableData dataWithCapacity: count + 4]; prefix = 0xC8; @@ -122,11 +123,11 @@ length = OFToBigEndian16((uint16_t)count); [ret addItems: &length count: 2]; [ret addItem: &_type]; - } else { + } else if (count <= UINT32_MAX) { uint32_t length; ret = [OFMutableData dataWithCapacity: count + 6]; prefix = 0xC9; @@ -134,11 +135,12 @@ length = OFToBigEndian32((uint32_t)count); [ret addItems: &length count: 4]; [ret addItem: &_type]; - } + } else + @throw [OFOutOfRangeException exception]; [ret addItems: _data.items count: _data.count]; [ret makeImmutable]; return ret;