Skip to content

Commit 4e615e3

Browse files
committed
(p)ereallocarray introduction.
Sort of php#8871 follow-up but on the zend part.
1 parent 1c753a9 commit 4e615e3

File tree

5 files changed

+21
-3
lines changed

5 files changed

+21
-3
lines changed

Zend/zend_alloc.c

+14
Original file line numberDiff line numberDiff line change
@@ -2632,6 +2632,11 @@ ZEND_API void* ZEND_FASTCALL _ecalloc(size_t nmemb, size_t size ZEND_FILE_LINE_D
26322632
return p;
26332633
}
26342634

2635+
ZEND_API void* ZEND_FASTCALL _ereallocarray(void* ptr, size_t nmemb, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
2636+
{
2637+
return safe_erealloc(ptr, nmemb, size, 0);
2638+
}
2639+
26352640
ZEND_API char* ZEND_FASTCALL _estrdup(const char *s ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
26362641
{
26372642
size_t length;
@@ -3111,6 +3116,15 @@ ZEND_API void * __zend_realloc(void *p, size_t len)
31113116
zend_out_of_memory();
31123117
}
31133118

3119+
ZEND_API void * __zend_reallocarray(void *p, size_t nmemb, size_t len)
3120+
{
3121+
void *tmp;
3122+
3123+
len = zend_safe_address_guarded(nmemb, len, 0);
3124+
tmp = __zend_realloc(p, len);
3125+
return tmp;
3126+
}
3127+
31143128
#ifdef ZTS
31153129
size_t zend_mm_globals_size(void)
31163130
{

Zend/zend_alloc.h

+4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ ZEND_API void ZEND_FASTCALL _efree(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_
7171
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);
7272
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);
7373
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);
74+
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);
7475
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);
7576
ZEND_API void* ZEND_FASTCALL _safe_realloc(void *ptr, size_t nmemb, size_t size, size_t offset);
7677
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);
158159
#define ecalloc(nmemb, size) _ecalloc((nmemb), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
159160
#define erealloc(ptr, size) _erealloc((ptr), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
160161
#define erealloc2(ptr, size, copy_size) _erealloc2((ptr), (size), (copy_size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
162+
#define ereallocarray(ptr, nmemb, size) _ereallocarray((ptr), (nmemb), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
161163
#define safe_erealloc(ptr, nmemb, size, offset) _safe_erealloc((ptr), (nmemb), (size), (offset) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
162164
#define erealloc_recoverable(ptr, size) _erealloc((ptr), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
163165
#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);
182184
ZEND_API void * __zend_malloc(size_t len) ZEND_ATTRIBUTE_MALLOC ZEND_ATTRIBUTE_ALLOC_SIZE(1);
183185
ZEND_API void * __zend_calloc(size_t nmemb, size_t len) ZEND_ATTRIBUTE_MALLOC ZEND_ATTRIBUTE_ALLOC_SIZE2(1,2);
184186
ZEND_API void * __zend_realloc(void *p, size_t len) ZEND_ATTRIBUTE_ALLOC_SIZE(2);
187+
ZEND_API void * __zend_reallocarray(void *p, size_t nmemb, size_t len) ZEND_ATTRIBUTE_ALLOC_SIZE2(2,3);
185188

186189
/* Selective persistent/non persistent allocation macros */
187190
#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)
198201
#define pecalloc(nmemb, size, persistent) ((persistent)?__zend_calloc((nmemb), (size)):ecalloc((nmemb), (size)))
199202
#define perealloc(ptr, size, persistent) ((persistent)?__zend_realloc((ptr), (size)):erealloc((ptr), (size)))
200203
#define perealloc2(ptr, size, copy_size, persistent) ((persistent)?__zend_realloc((ptr), (size)):erealloc2((ptr), (size), (copy_size)))
204+
#define pereallocarray(ptr, nmemb, size, persistent) ((persistent)?__zend_reallocarray((ptr), (nmemb), (size)):ereallocarray((ptr), (nmemb), (size)))
201205
#define safe_perealloc(ptr, nmemb, size, offset, persistent) ((persistent)?_safe_realloc((ptr), (nmemb), (size), (offset)):safe_erealloc((ptr), (nmemb), (size), (offset)))
202206
#define perealloc_recoverable(ptr, size, persistent) ((persistent)?realloc((ptr), (size)):erealloc_recoverable((ptr), (size)))
203207
#define perealloc2_recoverable(ptr, size, persistent) ((persistent)?realloc((ptr), (size)):erealloc2_recoverable((ptr), (size), (copy_size)))

Zend/zend_ptr_stack.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ END_EXTERN_C()
4848
do { \
4949
stack->max += PTR_STACK_BLOCK_SIZE; \
5050
} while (stack->top+count > stack->max); \
51-
stack->elements = (void **) perealloc(stack->elements, (sizeof(void *) * (stack->max)), stack->persistent); \
51+
stack->elements = (void **) pereallocarray(stack->elements, sizeof(void *), stack->max, stack->persistent); \
5252
stack->top_element = stack->elements+stack->top; \
5353
}
5454

ext/spl/spl_heap.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ static void spl_ptr_heap_insert(spl_ptr_heap *heap, void *elem, void *cmp_userda
278278
if (heap->count+1 > heap->max_size) {
279279
size_t alloc_size = heap->max_size * heap->elem_size;
280280
/* we need to allocate more memory */
281-
heap->elements = erealloc(heap->elements, 2 * alloc_size);
281+
heap->elements = ereallocarray(heap->elements, 2, alloc_size);
282282
memset((char *) heap->elements + alloc_size, 0, alloc_size);
283283
heap->max_size *= 2;
284284
}

ext/standard/basic_functions.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,7 @@ PHP_FUNCTION(getopt)
10991099

11001100
/* the first <len> slots are filled by the one short ops
11011101
* we now extend our array and jump to the new added structs */
1102-
opts = (opt_struct *) erealloc(opts, sizeof(opt_struct) * (len + count + 1));
1102+
opts = (opt_struct *) ereallocarray(opts, sizeof(opt_struct), (len + count + 1));
11031103
orig_opts = opts;
11041104
opts += len;
11051105

0 commit comments

Comments
 (0)