From b1d3cde23f0db79eeb1e128c33516bc5ab0ff4bc Mon Sep 17 00:00:00 2001 From: daanx Date: Sat, 6 May 2023 08:32:17 -0700 Subject: [PATCH] fix NULL check in realloc --- src/alloc.c | 2 ++ src/options.c | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/alloc.c b/src/alloc.c index a37b46b2..0ec418b2 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -709,6 +709,8 @@ static void* mi_heap_try_remap_zero(mi_heap_t* heap, mi_segment_t* segment, void void* _mi_heap_realloc_zero(mi_heap_t* heap, void* p, size_t newsize, bool zero) mi_attr_noexcept { // if p == NULL then behave as malloc. + if mi_unlikely(p==NULL) return _mi_heap_malloc_zero(heap,newsize,zero); + // else if size == 0 then reallocate to a zero-sized block (and don't return NULL, just as mi_malloc(0)). // (this means that returning NULL always indicates an error, and `p` will not have been freed in that case.) const size_t size = _mi_usable_size(p,"mi_realloc"); // also works if p == NULL (with size 0) diff --git a/src/options.c b/src/options.c index ba61f350..67d81317 100644 --- a/src/options.c +++ b/src/options.c @@ -91,7 +91,7 @@ static mi_option_desc_t options[_mi_option_last] = { 10, UNINIT, MI_OPTION(arena_purge_mult) }, // purge delay multiplier for arena's { 1, UNINIT, MI_OPTION_LEGACY(purge_extend_delay, decommit_extend_delay) }, - { 0, UNINIT, MI_OPTION(remap_threshold) }, // size in KiB after which realloc starts using OS remap (0 to disable auto remap) + { 0, UNINIT, MI_OPTION(remap_threshold) }, // size in KiB after which realloc starts using OS remap (0 to disable auto remap) }; static bool mi_option_is_size_in_kib(mi_option_t option) {