From 57408d83132e358bcdcd9731b86099947aa2c383 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Wed, 21 Feb 2018 20:25:30 +0800 Subject: [PATCH] Fix the potential leak of CFDataRef --- SDWebImage/UIImage+Transform.h | 3 ++- SDWebImage/UIImage+Transform.m | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/SDWebImage/UIImage+Transform.h b/SDWebImage/UIImage+Transform.h index 87a8a969..cbdbc212 100644 --- a/SDWebImage/UIImage+Transform.h +++ b/SDWebImage/UIImage+Transform.h @@ -124,7 +124,8 @@ typedef NS_OPTIONS(NSUInteger, SDRectCorner) { - (nullable UIImage *)sd_tintedImageWithColor:(nonnull UIColor *)tintColor; /** - Return the color at specify pixel. The postion is from the top-left to the bottom-right. And the color is always be RGBA format. + Return the color at specify pixel. The point is from the top-left to the bottom-right and 0-based. The returned the color is always be RGBA format. + @note The point's x/y should not be smaller than 0, or greater than or equal to width/height. @param point The position of pixel @return The color for specify pixel, or nil if any error occur diff --git a/SDWebImage/UIImage+Transform.m b/SDWebImage/UIImage+Transform.m index 5bcb6c83..7e66680a 100644 --- a/SDWebImage/UIImage+Transform.m +++ b/SDWebImage/UIImage+Transform.m @@ -396,7 +396,7 @@ static CGRect SDCGRectFitWithScaleMode(CGRect rect, CGSize size, SDImageScaleMod // Check point CGFloat width = CGImageGetWidth(imageRef); CGFloat height = CGImageGetHeight(imageRef); - if (point.x < 0 || point.y < 0 || point.x > width || point.y > height) { + if (point.x < 0 || point.y < 0 || point.x >= width || point.y >= height) { return nil; } @@ -411,15 +411,17 @@ static CGRect SDCGRectFitWithScaleMode(CGRect rect, CGSize size, SDImageScaleMod } // Get pixel at point - size_t bytesPerRow = CGImageGetBytesPerRow(imageRef); // Actually should be ARGB8888, equal to width * 4(alpha) or 3(non-alpha) + size_t bytesPerRow = CGImageGetBytesPerRow(imageRef); size_t components = CGImageGetBitsPerPixel(imageRef) / CGImageGetBitsPerComponent(imageRef); CFRange range = CFRangeMake(bytesPerRow * point.y + components * point.x, 4); if (CFDataGetLength(data) < range.location + range.length) { + CFRelease(data); return nil; } UInt8 pixel[4] = {0}; CFDataGetBytes(data, range, pixel); + CFRelease(data); // Convert to color CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);