From 3e0e45d313cabf8d646e97c1119383bce5f7d806 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Tue, 22 Jan 2019 15:17:02 +0800 Subject: [PATCH] Fix the potential leak of chunk iterator used for ICC Profile colorSpace check --- SDWebImageWebPCoder/Classes/SDImageWebPCoder.m | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/SDWebImageWebPCoder/Classes/SDImageWebPCoder.m b/SDWebImageWebPCoder/Classes/SDImageWebPCoder.m index 853f428..6fefb31 100644 --- a/SDWebImageWebPCoder/Classes/SDImageWebPCoder.m +++ b/SDWebImageWebPCoder/Classes/SDImageWebPCoder.m @@ -422,13 +422,17 @@ // WebP contains ICC Profile should use the desired colorspace, instead of default device colorspace // See: https://developers.google.com/speed/webp/docs/riff_container#color_profile - WebPChunkIterator chunk_iter; CGColorSpaceRef colorSpaceRef = NULL; + uint32_t flags = WebPDemuxGetI(demuxer, WEBP_FF_FORMAT_FLAGS); - int result = WebPDemuxGetChunk(demuxer, "ICCP", 1, &chunk_iter); - if (result) { - NSData *profileData = [NSData dataWithBytes:chunk_iter.chunk.bytes length:chunk_iter.chunk.size]; - colorSpaceRef = CGColorSpaceCreateWithICCProfile((__bridge CFDataRef)profileData); + if (flags & ICCP_FLAG) { + WebPChunkIterator chunk_iter; + int result = WebPDemuxGetChunk(demuxer, "ICCP", 1, &chunk_iter); + if (result) { + NSData *profileData = [NSData dataWithBytesNoCopy:(void *)chunk_iter.chunk.bytes length:chunk_iter.chunk.size freeWhenDone:NO]; + colorSpaceRef = CGColorSpaceCreateWithICCProfile((__bridge CFDataRef)profileData); + WebPDemuxReleaseChunkIterator(&chunk_iter); + } } if (!colorSpaceRef) {