Skip to content

Commit b38a0d8

Browse files
author
Mallory Paine
committed
Don't crash if mmap fails to map memory
1 parent 5e9c688 commit b38a0d8

2 files changed

Lines changed: 38 additions & 24 deletions

File tree

FastImageCache/FICImageTable.m

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,11 @@ - (FICImageTableChunk *)_chunkAtIndex:(NSInteger)index {
235235
}
236236
}
237237

238+
if (!chunk) {
239+
NSString *message = [NSString stringWithFormat:@"*** FIC Error: %s failed to get chunk for index %d.", __PRETTY_FUNCTION__, index];
240+
[[FICImageCache sharedImageCache] _logMessage:message];
241+
}
242+
238243
return chunk;
239244
}
240245

@@ -262,30 +267,32 @@ - (void)setEntryForEntityUUID:(NSString *)entityUUID sourceImageUUID:(NSString *
262267

263268
// Create context whose backing store *is* the mapped file data
264269
FICImageTableEntry *entryData = [self _entryDataAtIndex:newEntryIndex];
265-
CGContextRef context = CGBitmapContextCreate([entryData bytes], pixelSize.width, pixelSize.height, bitsPerComponent, _imageRowLength, colorSpace, bitmapInfo);
266-
CGColorSpaceRelease(colorSpace);
267-
268-
CGContextTranslateCTM(context, 0, pixelSize.height);
269-
CGContextScaleCTM(context, _screenScale, -_screenScale);
270-
271-
// Call drawing block to allow client to draw into the context
272-
imageDrawingBlock(context, [_imageFormat imageSize]);
273-
CGContextRelease(context);
274-
275-
[entryData setEntityUUIDBytes:FICUUIDBytesWithString(entityUUID)];
276-
[entryData setSourceImageUUIDBytes:FICUUIDBytesWithString(sourceImageUUID)];
277-
278-
// Update our book-keeping
279-
[_indexMap setObject:[NSNumber numberWithUnsignedInteger:newEntryIndex] forKey:entityUUID];
280-
[_occupiedIndexes addIndex:newEntryIndex];
281-
[_sourceImageMap setObject:sourceImageUUID forKey:entityUUID];
282-
283-
// Update MRU array
284-
[self _entryWasAccessedWithEntityUUID:entityUUID];
285-
[self saveMetadata];
286-
287-
// Write the data back to the filesystem
288-
[entryData flush];
270+
if (entryData) {
271+
CGContextRef context = CGBitmapContextCreate([entryData bytes], pixelSize.width, pixelSize.height, bitsPerComponent, _imageRowLength, colorSpace, bitmapInfo);
272+
CGColorSpaceRelease(colorSpace);
273+
274+
CGContextTranslateCTM(context, 0, pixelSize.height);
275+
CGContextScaleCTM(context, _screenScale, -_screenScale);
276+
277+
// Call drawing block to allow client to draw into the context
278+
imageDrawingBlock(context, [_imageFormat imageSize]);
279+
CGContextRelease(context);
280+
281+
[entryData setEntityUUIDBytes:FICUUIDBytesWithString(entityUUID)];
282+
[entryData setSourceImageUUIDBytes:FICUUIDBytesWithString(sourceImageUUID)];
283+
284+
// Update our book-keeping
285+
[_indexMap setObject:[NSNumber numberWithUnsignedInteger:newEntryIndex] forKey:entityUUID];
286+
[_occupiedIndexes addIndex:newEntryIndex];
287+
[_sourceImageMap setObject:sourceImageUUID forKey:entityUUID];
288+
289+
// Update MRU array
290+
[self _entryWasAccessedWithEntityUUID:entityUUID];
291+
[self saveMetadata];
292+
293+
// Write the data back to the filesystem
294+
[entryData flush];
295+
}
289296
}
290297

291298
[_lock unlock];
@@ -458,6 +465,11 @@ - (FICImageTableEntry *)_entryDataAtIndex:(NSInteger)index {
458465

459466
[_lock unlock];
460467

468+
if (!entryData) {
469+
NSString *message = [NSString stringWithFormat:@"*** FIC Error: %s failed to get entry for index %d.", __PRETTY_FUNCTION__, index];
470+
[[FICImageCache sharedImageCache] _logMessage:message];
471+
}
472+
461473
return entryData;
462474
}
463475

FastImageCache/FICImageTableChunk.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ - (instancetype)initWithFileDescriptor:(int)fileDescriptor index:(NSInteger)inde
4040
_bytes = mmap(NULL, _length, (PROT_READ|PROT_WRITE), (MAP_FILE|MAP_SHARED), fileDescriptor, _fileOffset);
4141

4242
if (_bytes == MAP_FAILED) {
43+
NSLog(@"Failed to map chunk. errno=%d", errno);
4344
_bytes = NULL;
45+
self = nil;
4446
}
4547
}
4648

0 commit comments

Comments
 (0)