Skip to content

Commit

Permalink
Add check for valid range on buffers
Browse files Browse the repository at this point in the history
Protects crashes on reading and writing
of buffer if range is accessed beyond
buffer size.
  • Loading branch information
Nadohs committed Mar 3, 2021
1 parent e70a147 commit 5f1bd9b
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions Cassette/CASQueueFile.m
Original file line number Diff line number Diff line change
Expand Up @@ -504,15 +504,23 @@ - (NSUInteger)usedBytes {
* Stores a 32-bit integer @c value in the @c buffer at the given @c offset.
*/
void writeInt(NSMutableData *buffer, NSUInteger offset, uint32_t value) {
[buffer replaceBytesInRange:NSMakeRange(offset, 4) withBytes:&value];
BOOL hasValidRange = buffer.length >= offset + 4;
NSCAssert(hasValidRange, @"write failed, range:(%lui,%lui) outside of buffer size:%lui", offset, offset+4, buffer.length);
if (hasValidRange) {
[buffer replaceBytesInRange:NSMakeRange(offset, 4) withBytes:&value];
}
}

/**
* Reads a 32-bit integer value from the @c buffer at @c offset.
*/
NSUInteger readUnsignedInt(NSData *buffer, NSUInteger offset) {
uint32_t value;
[buffer getBytes:&value range:NSMakeRange(offset, 4)];
uint32_t value = 0;
BOOL hasValidRange = buffer.length >= offset + 4;
NSCAssert(hasValidRange, @"read failed, range:(%lui,%lui) outside of buffer size:%lui", offset, offset+4, buffer.length);
if (hasValidRange) {
[buffer getBytes:&value range:NSMakeRange(offset, 4)];
}
return value;
}

Expand Down

0 comments on commit 5f1bd9b

Please sign in to comment.