From 2f5ac304a491797e0f20a5ab9dc79afddcdcb976 Mon Sep 17 00:00:00 2001 From: Sjoerd Langkemper Date: Wed, 21 Oct 2026 18:17:34 +0000 Subject: [PATCH] Zend: prevent malloc(0) in tracked allocator malloc(0) may return NULL. Prevent it by allocating at least one byte. Otherwise PHP may think the system is out of memory because malloc returns NULL. --- Zend/tests/zend_alloc_zero_size.phpt | 15 +++++++++++++++ Zend/zend_alloc.c | 4 ++++ 2 files changed, 19 insertions(+) create mode 100644 Zend/tests/zend_alloc_zero_size.phpt diff --git a/Zend/tests/zend_alloc_zero_size.phpt b/Zend/tests/zend_alloc_zero_size.phpt new file mode 100644 index 000000000000..6708ff31c7a3 --- /dev/null +++ b/Zend/tests/zend_alloc_zero_size.phpt @@ -0,0 +1,15 @@ +--TEST-- +Zero-size allocations with the tracked allocator +--EXTENSIONS-- +zend_test +--ENV-- +USE_ZEND_ALLOC=0 +USE_TRACKED_ALLOC=1 +--FILE-- + +--EXPECT-- +string(0) "" +string(0) "" diff --git a/Zend/zend_alloc.c b/Zend/zend_alloc.c index 3cbdd1e5f5dd..9bfa64bf2123 100644 --- a/Zend/zend_alloc.c +++ b/Zend/zend_alloc.c @@ -3291,6 +3291,10 @@ static zend_always_inline zend_result tracked_check_max_allocs(zend_mm_heap *hea static void *tracked_malloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) { zend_mm_heap *heap = AG(mm_heap); + + /* malloc(0) may return NULL, so allocate at least one byte. */ + size = MAX(size, 1); + if (UNEXPECTED(tracked_check_limit(heap, size ZEND_FILE_LINE_CC) == FAILURE)) { return NULL; }