Skip to content

Fix GH-19200: replace unchecked realloc/malloc with perealloc/pemalloc#21625

Open
iliaal wants to merge 2 commits intophp:PHP-8.4from
iliaal:fix/gh-19200-unchecked-realloc-malloc
Open

Fix GH-19200: replace unchecked realloc/malloc with perealloc/pemalloc#21625
iliaal wants to merge 2 commits intophp:PHP-8.4from
iliaal:fix/gh-19200-unchecked-realloc-malloc

Conversation

@iliaal
Copy link
Copy Markdown
Contributor

@iliaal iliaal commented Apr 3, 2026

Summary

Several call sites use raw realloc()/malloc() without checking for NULL. When realloc() fails, it returns NULL and the original pointer is lost, crashing on the next dereference. Replaced with perealloc()/pemalloc() (persistent=1), which wrap the system allocator and call zend_out_of_memory() on failure for a clean exit.

Changes:

  • Zend/zend.c, zend_append_version_info() realloc
  • Zend/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->interfaces realloc in 2 ZEND_INTERNAL_CLASS branches, simplified to single perealloc() matching the pattern at zend_implement_stringable()
  • ext/opcache/zend_accelerator_blacklist.c, blacklist entries realloc
  • main/network.c, gethostname_re() variants (3 mallocs + 3 reallocs)
  • main/php_ini.c, php_ini_scanned_files realloc
  • main/php_ini_builder.h, INI builder realloc
  • sapi/phpdbg/phpdbg.c, extension list realloc/malloc
  • sapi/phpdbg/phpdbg_prompt.c, code buffer realloc/malloc

Skipped zend_alloc.c (already handles this) and IR JIT code (third-party).

Fixes GH-19200, closes GH-17013

…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant