Fix GH-19200: replace unchecked realloc/malloc with perealloc/pemalloc#21625
Open
iliaal wants to merge 2 commits intophp:PHP-8.4from
Open
Fix GH-19200: replace unchecked realloc/malloc with perealloc/pemalloc#21625iliaal wants to merge 2 commits intophp:PHP-8.4from
iliaal wants to merge 2 commits intophp:PHP-8.4from
Conversation
…lloc Raw realloc() returns NULL on allocation failure, losing the original pointer and causing a crash on the next dereference. pemalloc/perealloc with persistent=1 wrap the system allocator but call zend_out_of_memory() on failure, giving a clean exit instead of an undefined crash. Converts all V701 locations from the PVS-Studio report and unchecked malloc calls in zend_register_functions() (phpGH-17013). Skips zend_alloc.c (already handled) and IR JIT code (third-party). The zend_inheritance.c changes also simplify the realloc/erealloc branch into a single perealloc() call, matching the existing pattern at zend_implement_stringable(). Fixes phpGH-19200 Closes phpGH-17013
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.
Summary
Several call sites use raw
realloc()/malloc()without checking for NULL. Whenrealloc()fails, it returns NULL and the original pointer is lost, crashing on the next dereference. Replaced withperealloc()/pemalloc()(persistent=1), which wrap the system allocator and callzend_out_of_memory()on failure for a clean exit.Changes:
Zend/zend.c,zend_append_version_info()reallocZend/zend_API.c,zend_collect_module_handlers()(3 reallocs),zend_register_functions()(2 reallocs + 3 mallocs),do_register_internal_class()(1 malloc)Zend/zend_inheritance.c,ce->interfacesrealloc in 2ZEND_INTERNAL_CLASSbranches, simplified to singleperealloc()matching the pattern atzend_implement_stringable()ext/opcache/zend_accelerator_blacklist.c, blacklist entries reallocmain/network.c,gethostname_re()variants (3 mallocs + 3 reallocs)main/php_ini.c,php_ini_scanned_filesreallocmain/php_ini_builder.h, INI builder reallocsapi/phpdbg/phpdbg.c, extension list realloc/mallocsapi/phpdbg/phpdbg_prompt.c, code buffer realloc/mallocSkipped
zend_alloc.c(already handles this) and IR JIT code (third-party).Fixes GH-19200, closes GH-17013