Changing the order of actions: the input argument is checked before memory is allocated.
The input argument `n` is of type `size_t` and cannot take negative values, so the comparison has been fixed. Signed-off-by: Maks Mishin <maks.mishi...@gmail.com> --- common/dlmalloc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/common/dlmalloc.c b/common/dlmalloc.c index cc4d3a0a02..2f4cabda4a 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -2093,6 +2093,8 @@ Void_t* cALLOc_impl(n, elem_size) size_t n; size_t elem_size; mchunkptr p; INTERNAL_SIZE_T csz; + if ((long)n <= 0) return NULL; + INTERNAL_SIZE_T sz = n * elem_size; /* check if expand_top called, in which case don't need to clear */ @@ -2104,8 +2106,6 @@ Void_t* cALLOc_impl(n, elem_size) size_t n; size_t elem_size; #endif Void_t* mem = mALLOc_impl (sz); - if ((long)n < 0) return NULL; - if (mem == NULL) return NULL; else -- 2.34.1