Add ImageIO related C nullable check to prevent crash (#23186)

Summary:
Changelog:
----------

[iOS] [Fixed] - Add ImageIO related C nullable check to prevent crash
Pull Request resolved: https://github.com/facebook/react-native/pull/23186

Differential Revision: D13838590

Pulled By: cpojer

fbshipit-source-id: 14bfa826ce75c32129e6a980a04bb85fb35411a0
This commit is contained in:
zhongwuzw 2019-01-28 03:24:50 -08:00 committed by Facebook Github Bot
parent 959a13363a
commit d0cd3cae13
1 changed files with 11 additions and 4 deletions

View File

@ -31,6 +31,10 @@ RCT_EXPORT_MODULE()
completionHandler:(RCTImageLoaderCompletionBlock)completionHandler completionHandler:(RCTImageLoaderCompletionBlock)completionHandler
{ {
CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)imageData, NULL); CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)imageData, NULL);
if (!imageSource) {
completionHandler(nil, nil);
return ^{};
}
NSDictionary<NSString *, id> *properties = (__bridge_transfer NSDictionary *)CGImageSourceCopyProperties(imageSource, NULL); NSDictionary<NSString *, id> *properties = (__bridge_transfer NSDictionary *)CGImageSourceCopyProperties(imageSource, NULL);
CGFloat loopCount = 0; CGFloat loopCount = 0;
if ([[properties[(id)kCGImagePropertyGIFDictionary] allKeys] containsObject:(id)kCGImagePropertyGIFLoopCount]) { if ([[properties[(id)kCGImagePropertyGIFDictionary] allKeys] containsObject:(id)kCGImagePropertyGIFLoopCount]) {
@ -54,6 +58,9 @@ RCT_EXPORT_MODULE()
for (size_t i = 0; i < imageCount; i++) { for (size_t i = 0; i < imageCount; i++) {
CGImageRef imageRef = CGImageSourceCreateImageAtIndex(imageSource, i, NULL); CGImageRef imageRef = CGImageSourceCreateImageAtIndex(imageSource, i, NULL);
if (!imageRef) {
continue;
}
if (!image) { if (!image) {
image = [UIImage imageWithCGImage:imageRef scale:scale orientation:UIImageOrientationUp]; image = [UIImage imageWithCGImage:imageRef scale:scale orientation:UIImageOrientationUp];
} }
@ -64,10 +71,10 @@ RCT_EXPORT_MODULE()
const NSTimeInterval kDelayTimeIntervalDefault = 0.1; const NSTimeInterval kDelayTimeIntervalDefault = 0.1;
NSNumber *delayTime = frameGIFProperties[(id)kCGImagePropertyGIFUnclampedDelayTime] ?: frameGIFProperties[(id)kCGImagePropertyGIFDelayTime]; NSNumber *delayTime = frameGIFProperties[(id)kCGImagePropertyGIFUnclampedDelayTime] ?: frameGIFProperties[(id)kCGImagePropertyGIFDelayTime];
if (delayTime == nil) { if (delayTime == nil) {
if (i == 0) { if (delays.count == 0) {
delayTime = @(kDelayTimeIntervalDefault); delayTime = @(kDelayTimeIntervalDefault);
} else { } else {
delayTime = delays[i - 1]; delayTime = delays.lastObject;
} }
} }
@ -77,8 +84,8 @@ RCT_EXPORT_MODULE()
} }
duration += delayTime.doubleValue; duration += delayTime.doubleValue;
delays[i] = delayTime; [delays addObject:delayTime];
images[i] = (__bridge_transfer id)imageRef; [images addObject:(__bridge_transfer id)imageRef];
} }
CFRelease(imageSource); CFRelease(imageSource);