Index: src/OFStream.m ================================================================== --- src/OFStream.m +++ src/OFStream.m @@ -290,11 +290,16 @@ } - (size_t)readBigEndianInt16sIntoBuffer: (uint16_t *)buffer count: (size_t)count { - size_t size = count * sizeof(uint16_t); + size_t size; + + if OF_UNLIKELY (count > SIZE_MAX / sizeof(uint16_t)) + @throw [OFOutOfRangeException exception]; + + size = count * sizeof(uint16_t); [self readIntoBuffer: buffer exactLength: size]; #ifndef OF_BIG_ENDIAN @@ -306,11 +311,16 @@ } - (size_t)readBigEndianInt32sIntoBuffer: (uint32_t *)buffer count: (size_t)count { - size_t size = count * sizeof(uint32_t); + size_t size; + + if OF_UNLIKELY (count > SIZE_MAX / sizeof(uint32_t)) + @throw [OFOutOfRangeException exception]; + + size = count * sizeof(uint32_t); [self readIntoBuffer: buffer exactLength: size]; #ifndef OF_BIG_ENDIAN @@ -322,11 +332,16 @@ } - (size_t)readBigEndianInt64sIntoBuffer: (uint64_t *)buffer count: (size_t)count { - size_t size = count * sizeof(uint64_t); + size_t size; + + if OF_UNLIKELY (count > SIZE_MAX / sizeof(uint64_t)) + @throw [OFOutOfRangeException exception]; + + size = count * sizeof(uint64_t); [self readIntoBuffer: buffer exactLength: size]; #ifndef OF_BIG_ENDIAN @@ -338,11 +353,16 @@ } - (size_t)readBigEndianFloatsIntoBuffer: (float *)buffer count: (size_t)count { - size_t size = count * sizeof(float); + size_t size; + + if OF_UNLIKELY (count > SIZE_MAX / sizeof(float)) + @throw [OFOutOfRangeException exception]; + + size = count * sizeof(float); [self readIntoBuffer: buffer exactLength: size]; #ifndef OF_FLOAT_BIG_ENDIAN @@ -354,11 +374,16 @@ } - (size_t)readBigEndianDoublesIntoBuffer: (double *)buffer count: (size_t)count { - size_t size = count * sizeof(double); + size_t size; + + if OF_UNLIKELY (count > SIZE_MAX / sizeof(double)) + @throw [OFOutOfRangeException exception]; + + size = count * sizeof(double); [self readIntoBuffer: buffer exactLength: size]; #ifndef OF_FLOAT_BIG_ENDIAN @@ -420,11 +445,16 @@ } - (size_t)readLittleEndianInt16sIntoBuffer: (uint16_t *)buffer count: (size_t)count { - size_t size = count * sizeof(uint16_t); + size_t size; + + if OF_UNLIKELY (count > SIZE_MAX / sizeof(uint16_t)) + @throw [OFOutOfRangeException exception]; + + size = count * sizeof(uint16_t); [self readIntoBuffer: buffer exactLength: size]; #ifdef OF_BIG_ENDIAN @@ -436,11 +466,16 @@ } - (size_t)readLittleEndianInt32sIntoBuffer: (uint32_t *)buffer count: (size_t)count { - size_t size = count * sizeof(uint32_t); + size_t size; + + if OF_UNLIKELY (count > SIZE_MAX / sizeof(uint32_t)) + @throw [OFOutOfRangeException exception]; + + size = count * sizeof(uint32_t); [self readIntoBuffer: buffer exactLength: size]; #ifdef OF_BIG_ENDIAN @@ -452,11 +487,16 @@ } - (size_t)readLittleEndianInt64sIntoBuffer: (uint64_t *)buffer count: (size_t)count { - size_t size = count * sizeof(uint64_t); + size_t size; + + if OF_UNLIKELY (count > SIZE_MAX / sizeof(uint64_t)) + @throw [OFOutOfRangeException exception]; + + size = count * sizeof(uint64_t); [self readIntoBuffer: buffer exactLength: size]; #ifdef OF_BIG_ENDIAN @@ -468,11 +508,16 @@ } - (size_t)readLittleEndianFloatsIntoBuffer: (float *)buffer count: (size_t)count { - size_t size = count * sizeof(float); + size_t size; + + if OF_UNLIKELY (count > SIZE_MAX / sizeof(float)) + @throw [OFOutOfRangeException exception]; + + size = count * sizeof(float); [self readIntoBuffer: buffer exactLength: size]; #ifdef OF_FLOAT_BIG_ENDIAN @@ -484,11 +529,16 @@ } - (size_t)readLittleEndianDoublesIntoBuffer: (double *)buffer count: (size_t)count { - size_t size = count * sizeof(double); + size_t size; + + if OF_UNLIKELY (count > SIZE_MAX / sizeof(double)) + @throw [OFOutOfRangeException exception]; + + size = count * sizeof(double); [self readIntoBuffer: buffer exactLength: size]; #ifdef OF_FLOAT_BIG_ENDIAN @@ -1039,11 +1089,16 @@ } - (size_t)writeBigEndianInt16s: (const uint16_t *)buffer count: (size_t)count { - size_t size = count * sizeof(uint16_t); + size_t size; + + if OF_UNLIKELY (count > SIZE_MAX / sizeof(uint16_t)) + @throw [OFOutOfRangeException exception]; + + size = count * sizeof(uint16_t); #ifdef OF_BIG_ENDIAN [self writeBuffer: buffer length: size]; #else @@ -1065,11 +1120,16 @@ } - (size_t)writeBigEndianInt32s: (const uint32_t *)buffer count: (size_t)count { - size_t size = count * sizeof(uint32_t); + size_t size; + + if OF_UNLIKELY (count > SIZE_MAX / sizeof(uint32_t)) + @throw [OFOutOfRangeException exception]; + + size = count * sizeof(uint32_t); #ifdef OF_BIG_ENDIAN [self writeBuffer: buffer length: size]; #else @@ -1091,11 +1151,16 @@ } - (size_t)writeBigEndianInt64s: (const uint64_t *)buffer count: (size_t)count { - size_t size = count * sizeof(uint64_t); + size_t size; + + if OF_UNLIKELY (count > SIZE_MAX / sizeof(uint64_t)) + @throw [OFOutOfRangeException exception]; + + size = count * sizeof(uint64_t); #ifdef OF_BIG_ENDIAN [self writeBuffer: buffer length: size]; #else @@ -1117,11 +1182,16 @@ } - (size_t)writeBigEndianFloats: (const float *)buffer count: (size_t)count { - size_t size = count * sizeof(float); + size_t size; + + if OF_UNLIKELY (count > SIZE_MAX / sizeof(float)) + @throw [OFOutOfRangeException exception]; + + size = count * sizeof(float); #ifdef OF_FLOAT_BIG_ENDIAN [self writeBuffer: buffer length: size]; #else @@ -1143,11 +1213,16 @@ } - (size_t)writeBigEndianDoubles: (const double *)buffer count: (size_t)count { - size_t size = count * sizeof(double); + size_t size; + + if OF_UNLIKELY (count > SIZE_MAX / sizeof(double)) + @throw [OFOutOfRangeException exception]; + + size = count * sizeof(double); #ifdef OF_FLOAT_BIG_ENDIAN [self writeBuffer: buffer length: size]; #else @@ -1209,11 +1284,16 @@ } - (size_t)writeLittleEndianInt16s: (const uint16_t *)buffer count: (size_t)count { - size_t size = count * sizeof(uint16_t); + size_t size; + + if OF_UNLIKELY (count > SIZE_MAX / sizeof(uint16_t)) + @throw [OFOutOfRangeException exception]; + + size = count * sizeof(uint16_t); #ifndef OF_BIG_ENDIAN [self writeBuffer: buffer length: size]; #else @@ -1235,11 +1315,16 @@ } - (size_t)writeLittleEndianInt32s: (const uint32_t *)buffer count: (size_t)count { - size_t size = count * sizeof(uint32_t); + size_t size; + + if OF_UNLIKELY (count > SIZE_MAX / sizeof(uint32_t)) + @throw [OFOutOfRangeException exception]; + + size = count * sizeof(uint32_t); #ifndef OF_BIG_ENDIAN [self writeBuffer: buffer length: size]; #else @@ -1261,11 +1346,16 @@ } - (size_t)writeLittleEndianInt64s: (const uint64_t *)buffer count: (size_t)count { - size_t size = count * sizeof(uint64_t); + size_t size; + + if OF_UNLIKELY (count > SIZE_MAX / sizeof(uint64_t)) + @throw [OFOutOfRangeException exception]; + + size = count * sizeof(uint64_t); #ifndef OF_BIG_ENDIAN [self writeBuffer: buffer length: size]; #else @@ -1287,11 +1377,16 @@ } - (size_t)writeLittleEndianFloats: (const float *)buffer count: (size_t)count { - size_t size = count * sizeof(float); + size_t size; + + if OF_UNLIKELY (count > SIZE_MAX / sizeof(float)) + @throw [OFOutOfRangeException exception]; + + size = count * sizeof(float); #ifndef OF_FLOAT_BIG_ENDIAN [self writeBuffer: buffer length: size]; #else @@ -1313,11 +1408,16 @@ } - (size_t)writeLittleEndianDoubles: (const double *)buffer count: (size_t)count { - size_t size = count * sizeof(double); + size_t size; + + if OF_UNLIKELY (count > SIZE_MAX / sizeof(double)) + @throw [OFOutOfRangeException exception]; + + size = count * sizeof(double); #ifndef OF_FLOAT_BIG_ENDIAN [self writeBuffer: buffer length: size]; #else