data corruption bug in HEIC coder
@dlemstra Hi.
I think there is a memory corruption bug in the HEIC coder.
https://github.com/ImageMagick/ImageMagick6/blob/9076ea800b5ec8143439b4f2b025d546ce8960a5/coders/heic.c#L208
Currently, ImageMagick frees (relinquish) an allocated memory (containing a HEIF image file) right after initializing a heif_context variable. The current version uses "heif_context_read_from_memory_without_copy" (which simply sets a given memory pointer as the source of an image) not "heif_context_read_from_memory". Here, HEIC decoder functions (in libheif library) refer the given memory pointer (which is allocated from AcquireMagickMemory and immediately freed); therefore, if the given memory region is overwritten by other memory allocations, data (raw HEIC image) will be corrupted.
To fix this problem, I think we have two options.
-
use "heif_context_read_from_memory", not "heif_context_read_from_memory_without_copy".
-
remove a "RelinquishMagickMemory" function in the line number 208
======== I found the commit switching to "heif_context_read_from_memory_without_copy" as some methods has been deprecated. (looks like "heif_context_read_from_memory" is deprecated??) (https://github.com/ImageMagick/ImageMagick6/commit/665b5fd9173a26b85c8b6e1fd487376fd2dc5f60)
So I recommend the option 2. (remove the invalid free operation causing memory corruption)
Thanks.
Thanks for the problem report. We can reproduce it and will have a patch to fix it in GIT master branch @ https://github.com/ImageMagick/ImageMagick later today. The patch will be available in the beta releases of ImageMagick @ https://www.imagemagick.org/download/beta/ by sometime tomorrow.
As the problem is solved in the recent version, I close this issue. Thanks.