From c42678344ebfdaa950a27b4b7b098bb2551b8e5f Mon Sep 17 00:00:00 2001 From: David Carlier Date: Mon, 27 Jun 2022 19:30:08 +0100 Subject: [PATCH] `(p)ereallocarray` introduction. Sort of #8871 follow-up but on the zend part. --- Zend/zend_alloc.c | 22 ++++++++++++++++++++++ Zend/zend_alloc.h | 4 ++++ Zend/zend_ptr_stack.h | 2 +- ext/spl/spl_heap.c | 2 +- ext/standard/basic_functions.c | 2 +- 5 files changed, 29 insertions(+), 3 deletions(-) diff --git a/Zend/zend_alloc.c b/Zend/zend_alloc.c index c7895a6624c60..540dfe128a7a5 100644 --- a/Zend/zend_alloc.c +++ b/Zend/zend_alloc.c @@ -2632,6 +2632,19 @@ ZEND_API void* ZEND_FASTCALL _ecalloc(size_t nmemb, size_t size ZEND_FILE_LINE_D return p; } +ZEND_API void* ZEND_FASTCALL _ereallocarray(void* ptr, size_t nmemb, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) +{ + void *p; + + size = zend_safe_address_guarded(nmemb, size, 0); + + if (!size) { + return NULL; + } + p = _erealloc(ptr, size ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_ORIG_RELAY_CC); + return p; +} + ZEND_API char* ZEND_FASTCALL _estrdup(const char *s ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) { size_t length; @@ -3111,6 +3124,15 @@ ZEND_API void * __zend_realloc(void *p, size_t len) zend_out_of_memory(); } +ZEND_API void * __zend_reallocarray(void *p, size_t nmemb, size_t len) +{ + void *tmp; + + len = zend_safe_address_guarded(nmemb, len, 0); + tmp = __zend_realloc(p, len); + return tmp; +} + #ifdef ZTS size_t zend_mm_globals_size(void) { diff --git a/Zend/zend_alloc.h b/Zend/zend_alloc.h index 9e10e6971f810..07d24149732a5 100644 --- a/Zend/zend_alloc.h +++ b/Zend/zend_alloc.h @@ -71,6 +71,7 @@ ZEND_API void ZEND_FASTCALL _efree(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ ZEND_API void* ZEND_FASTCALL _ecalloc(size_t nmemb, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC ZEND_ATTRIBUTE_ALLOC_SIZE2(1,2); ZEND_API void* ZEND_FASTCALL _erealloc(void *ptr, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_ALLOC_SIZE(2); ZEND_API void* ZEND_FASTCALL _erealloc2(void *ptr, size_t size, size_t copy_size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_ALLOC_SIZE(2); +ZEND_API void* ZEND_FASTCALL _ereallocarray(void *ptr, size_t nmemb, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC ZEND_ATTRIBUTE_ALLOC_SIZE2(2,3); ZEND_API void* ZEND_FASTCALL _safe_erealloc(void *ptr, size_t nmemb, size_t size, size_t offset ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC); ZEND_API void* ZEND_FASTCALL _safe_realloc(void *ptr, size_t nmemb, size_t size, size_t offset); ZEND_API char* ZEND_FASTCALL _estrdup(const char *s ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC; @@ -158,6 +159,7 @@ ZEND_API void ZEND_FASTCALL _efree_huge(void *, size_t size); #define ecalloc(nmemb, size) _ecalloc((nmemb), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC) #define erealloc(ptr, size) _erealloc((ptr), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC) #define erealloc2(ptr, size, copy_size) _erealloc2((ptr), (size), (copy_size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC) +#define ereallocarray(ptr, nmemb, size) _ereallocarray((ptr), (nmemb), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC) #define safe_erealloc(ptr, nmemb, size, offset) _safe_erealloc((ptr), (nmemb), (size), (offset) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC) #define erealloc_recoverable(ptr, size) _erealloc((ptr), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC) #define erealloc2_recoverable(ptr, size, copy_size) _erealloc2((ptr), (size), (copy_size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC) @@ -182,6 +184,7 @@ ZEND_API void ZEND_FASTCALL _efree_huge(void *, size_t size); ZEND_API void * __zend_malloc(size_t len) ZEND_ATTRIBUTE_MALLOC ZEND_ATTRIBUTE_ALLOC_SIZE(1); ZEND_API void * __zend_calloc(size_t nmemb, size_t len) ZEND_ATTRIBUTE_MALLOC ZEND_ATTRIBUTE_ALLOC_SIZE2(1,2); ZEND_API void * __zend_realloc(void *p, size_t len) ZEND_ATTRIBUTE_ALLOC_SIZE(2); +ZEND_API void * __zend_reallocarray(void *p, size_t nmemb, size_t len) ZEND_ATTRIBUTE_ALLOC_SIZE2(2,3); /* Selective persistent/non persistent allocation macros */ #define pemalloc(size, persistent) ((persistent)?__zend_malloc(size):emalloc(size)) @@ -198,6 +201,7 @@ ZEND_API void * __zend_realloc(void *p, size_t len) ZEND_ATTRIBUTE_ALLOC_SIZE(2) #define pecalloc(nmemb, size, persistent) ((persistent)?__zend_calloc((nmemb), (size)):ecalloc((nmemb), (size))) #define perealloc(ptr, size, persistent) ((persistent)?__zend_realloc((ptr), (size)):erealloc((ptr), (size))) #define perealloc2(ptr, size, copy_size, persistent) ((persistent)?__zend_realloc((ptr), (size)):erealloc2((ptr), (size), (copy_size))) +#define pereallocarray(ptr, nmemb, size, persistent) ((persistent)?__zend_reallocarray((ptr), (nmemb), (size)):ereallocarray((ptr), (nmemb), (size))) #define safe_perealloc(ptr, nmemb, size, offset, persistent) ((persistent)?_safe_realloc((ptr), (nmemb), (size), (offset)):safe_erealloc((ptr), (nmemb), (size), (offset))) #define perealloc_recoverable(ptr, size, persistent) ((persistent)?realloc((ptr), (size)):erealloc_recoverable((ptr), (size))) #define perealloc2_recoverable(ptr, size, persistent) ((persistent)?realloc((ptr), (size)):erealloc2_recoverable((ptr), (size), (copy_size))) diff --git a/Zend/zend_ptr_stack.h b/Zend/zend_ptr_stack.h index fd4b59d15178c..890405afa5210 100644 --- a/Zend/zend_ptr_stack.h +++ b/Zend/zend_ptr_stack.h @@ -48,7 +48,7 @@ END_EXTERN_C() do { \ stack->max += PTR_STACK_BLOCK_SIZE; \ } while (stack->top+count > stack->max); \ - stack->elements = (void **) perealloc(stack->elements, (sizeof(void *) * (stack->max)), stack->persistent); \ + stack->elements = (void **) pereallocarray(stack->elements, sizeof(void *), stack->max, stack->persistent); \ stack->top_element = stack->elements+stack->top; \ } diff --git a/ext/spl/spl_heap.c b/ext/spl/spl_heap.c index c541eb3c47416..4b6e5822cd581 100644 --- a/ext/spl/spl_heap.c +++ b/ext/spl/spl_heap.c @@ -278,7 +278,7 @@ static void spl_ptr_heap_insert(spl_ptr_heap *heap, void *elem, void *cmp_userda if (heap->count+1 > heap->max_size) { size_t alloc_size = heap->max_size * heap->elem_size; /* we need to allocate more memory */ - heap->elements = erealloc(heap->elements, 2 * alloc_size); + heap->elements = ereallocarray(heap->elements, 2, alloc_size); memset((char *) heap->elements + alloc_size, 0, alloc_size); heap->max_size *= 2; } diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 5123174569fa3..f5c9cb737d07f 100755 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -1099,7 +1099,7 @@ PHP_FUNCTION(getopt) /* the first slots are filled by the one short ops * we now extend our array and jump to the new added structs */ - opts = (opt_struct *) erealloc(opts, sizeof(opt_struct) * (len + count + 1)); + opts = (opt_struct *) ereallocarray(opts, sizeof(opt_struct), (len + count + 1)); orig_opts = opts; opts += len;