From f8276805ca33fdac962e50a854841a3f625a2f7c Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Tue, 19 Feb 2019 21:16:36 -0800 Subject: [PATCH] Add ImageIO nullable check in Image module (#23544) Summary: Add ImageIO nullable check in Image module to prevent crash. [iOS] [Fixed] - Add ImageIO nullable check in Image module Pull Request resolved: https://github.com/facebook/react-native/pull/23544 Differential Revision: D14146483 Pulled By: cpojer fbshipit-source-id: 813caebea1e24d5e282b21cc50240ec1886339d5 --- Libraries/Image/RCTImageUtils.m | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Libraries/Image/RCTImageUtils.m b/Libraries/Image/RCTImageUtils.m index 5470c8f78..0d2e1df1e 100644 --- a/Libraries/Image/RCTImageUtils.m +++ b/Libraries/Image/RCTImageUtils.m @@ -330,12 +330,15 @@ NSDictionary *__nullable RCTGetImageMetadata(NSData *data) NSData *__nullable RCTGetImageData(UIImage *image, float quality) { + CGImageRef cgImage = image.CGImage; + if (!cgImage) { + return NULL; + } NSMutableDictionary *properties = [[NSMutableDictionary alloc] initWithDictionary:@{ (id)kCGImagePropertyOrientation : @(CGImagePropertyOrientationFromUIImageOrientation(image.imageOrientation)) }]; CGImageDestinationRef destination; CFMutableDataRef imageData = CFDataCreateMutable(NULL, 0); - CGImageRef cgImage = image.CGImage; if (RCTImageHasAlpha(cgImage)) { // get png data destination = CGImageDestinationCreateWithData(imageData, kUTTypePNG, 1, NULL); @@ -344,9 +347,12 @@ NSData *__nullable RCTGetImageData(UIImage *image, float quality) destination = CGImageDestinationCreateWithData(imageData, kUTTypeJPEG, 1, NULL); [properties setValue:@(quality) forKey:(id)kCGImageDestinationLossyCompressionQuality]; } + if (!destination) { + CFRelease(imageData); + return NULL; + } CGImageDestinationAddImage(destination, cgImage, (__bridge CFDictionaryRef)properties); - if (!CGImageDestinationFinalize(destination)) - { + if (!CGImageDestinationFinalize(destination)) { CFRelease(imageData); imageData = NULL; }