Skip to content

Commit 7044ad1

Browse files
author
Mallory Paine
committed
Add safety check so that we cannot create an entry that is out-of-bounds within a chunk
1 parent 21b6cb0 commit 7044ad1

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

FastImageCache/FICImageTable.m

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -487,14 +487,16 @@ - (FICImageTableEntry *)_entryDataAtIndex:(NSInteger)index {
487487
void *mappedChunkAddress = [chunk bytes];
488488
void *mappedEntryAddress = mappedChunkAddress + entryOffsetInChunk;
489489
entryData = [[FICImageTableEntry alloc] initWithImageTableChunk:chunk bytes:mappedEntryAddress length:_entryLength];
490-
[entryData setIndex:index];
491490

492-
[_chunkSet addObject:chunk];
491+
if (entryData) {
492+
[entryData setIndex:index];
493+
[_chunkSet addObject:chunk];
493494

494-
__weak FICImageTable *weakSelf = self;
495-
[entryData executeBlockOnDealloc:^{
496-
[weakSelf _entryWasDeallocatedFromChunk:chunk];
497-
}];
495+
__weak FICImageTable *weakSelf = self;
496+
[entryData executeBlockOnDealloc:^{
497+
[weakSelf _entryWasDeallocatedFromChunk:chunk];
498+
}];
499+
}
498500
}
499501
}
500502

FastImageCache/FICImageTableEntry.m

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,17 @@ - (id)initWithImageTableChunk:(FICImageTableChunk *)imageTableChunk bytes:(void
6464
self = [super init];
6565

6666
if (self != nil) {
67-
_imageTableChunk = imageTableChunk;
68-
_bytes = bytes;
69-
_length = length;
70-
_deallocBlocks = [[NSMutableArray alloc] init];
67+
// Safety check
68+
void *entryMax = bytes + length;
69+
void *chunkMax = [imageTableChunk bytes] + [imageTableChunk length];
70+
if (entryMax > chunkMax) {
71+
self = nil;
72+
} else {
73+
_imageTableChunk = imageTableChunk;
74+
_bytes = bytes;
75+
_length = length;
76+
_deallocBlocks = [[NSMutableArray alloc] init];
77+
}
7178
}
7279

7380
return self;

0 commit comments

Comments
 (0)