Fix memory leak (and heap corruption) in getDensity() function#787
Open
ndossche wants to merge 1 commit into
Open
Fix memory leak (and heap corruption) in getDensity() function#787ndossche wants to merge 1 commit into
ndossche wants to merge 1 commit into
Conversation
Memory returned by `DrawGetDensity()` must be freed by
`MagickRelinquishMemory()`.
This also means that the PHP 5-era code will cause heap corruption as it
doesn't duplicate the string and will free memory via ZendMM even though
the memory doesn't come from ZendMM.
Fix the macros and fix the missing free.
ASAN report:
```
Direct leak of 4099 byte(s) in 1 object(s) allocated from:
#0 0x77ebf1bc59c7 in malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:69
Imagick#1 0x77ebeb7ad28b in AcquireString MagickCore/string.c:109
Imagick#2 0x77ebeca40c9a in zim_ImagickDraw_getDensity /work/php-imagemagick/imagickdraw_class.c:3067
Imagick#3 0x5bd4b54c9395 in zend_test_execute_internal /work/php-src/ext/zend_test/observer.c:306
Imagick#4 0x5bd4b57f170a in ZEND_DO_FCALL_SPEC_RETVAL_USED_HANDLER /work/php-src/Zend/zend_vm_execute.h:2154
Imagick#5 0x5bd4b5951e55 in execute_ex /work/php-src/Zend/zend_vm_execute.h:116519
Imagick#6 0x5bd4b5966d70 in zend_execute /work/php-src/Zend/zend_vm_execute.h:121962
Imagick#7 0x5bd4b5acb56b in zend_execute_script /work/php-src/Zend/zend.c:1980
Imagick#8 0x5bd4b54fdd7b in php_execute_script_ex /work/php-src/main/main.c:2645
Imagick#9 0x5bd4b54fe18b in php_execute_script /work/php-src/main/main.c:2685
Imagick#10 0x5bd4b5ad10d6 in do_cli /work/php-src/sapi/cli/php_cli.c:951
Imagick#11 0x5bd4b5ad36a3 in main /work/php-src/sapi/cli/php_cli.c:1362
Imagick#12 0x77ebf0ea31c9 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
Imagick#13 0x77ebf0ea328a in __libc_start_main_impl ../csu/libc-start.c:360
Imagick#14 0x5bd4b4609df4 in _start (/work/php-src/build-dbg-asan/sapi/cli/php+0x609df4) (BuildId: 97494815ba6ad97379608f28619e331873dc4434)
```
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Memory returned by
DrawGetDensity()must be freed byMagickRelinquishMemory().This also means that the PHP 5-era code will cause heap corruption as it doesn't duplicate the string and will free memory via ZendMM even though the memory doesn't come from ZendMM.
Fix the macros and fix the missing free.
ASAN report:
Note: this was found by a hybrid static-dynamic analyzer I'm developing.