From d0cd3cae13a1b1fff8a2e378b5228d3cdccd695f Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Mon, 28 Jan 2019 03:24:50 -0800 Subject: [PATCH] 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 --- Libraries/Image/RCTGIFImageDecoder.m | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Libraries/Image/RCTGIFImageDecoder.m b/Libraries/Image/RCTGIFImageDecoder.m index 73fef5381a7c38..a0afd43f4da45a 100644 --- a/Libraries/Image/RCTGIFImageDecoder.m +++ b/Libraries/Image/RCTGIFImageDecoder.m @@ -31,6 +31,10 @@ - (RCTImageLoaderCancellationBlock)decodeImageData:(NSData *)imageData completionHandler:(RCTImageLoaderCompletionBlock)completionHandler { CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)imageData, NULL); + if (!imageSource) { + completionHandler(nil, nil); + return ^{}; + } NSDictionary *properties = (__bridge_transfer NSDictionary *)CGImageSourceCopyProperties(imageSource, NULL); CGFloat loopCount = 0; if ([[properties[(id)kCGImagePropertyGIFDictionary] allKeys] containsObject:(id)kCGImagePropertyGIFLoopCount]) { @@ -54,6 +58,9 @@ - (RCTImageLoaderCancellationBlock)decodeImageData:(NSData *)imageData for (size_t i = 0; i < imageCount; i++) { CGImageRef imageRef = CGImageSourceCreateImageAtIndex(imageSource, i, NULL); + if (!imageRef) { + continue; + } if (!image) { image = [UIImage imageWithCGImage:imageRef scale:scale orientation:UIImageOrientationUp]; } @@ -64,10 +71,10 @@ - (RCTImageLoaderCancellationBlock)decodeImageData:(NSData *)imageData const NSTimeInterval kDelayTimeIntervalDefault = 0.1; NSNumber *delayTime = frameGIFProperties[(id)kCGImagePropertyGIFUnclampedDelayTime] ?: frameGIFProperties[(id)kCGImagePropertyGIFDelayTime]; if (delayTime == nil) { - if (i == 0) { + if (delays.count == 0) { delayTime = @(kDelayTimeIntervalDefault); } else { - delayTime = delays[i - 1]; + delayTime = delays.lastObject; } } @@ -77,8 +84,8 @@ - (RCTImageLoaderCancellationBlock)decodeImageData:(NSData *)imageData } duration += delayTime.doubleValue; - delays[i] = delayTime; - images[i] = (__bridge_transfer id)imageRef; + [delays addObject:delayTime]; + [images addObject:(__bridge_transfer id)imageRef]; } CFRelease(imageSource);