From ba7802315dd06885ffcd4c1871612bf96e4b15bf Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 17 Oct 2018 00:14:50 +0200 Subject: [PATCH] unix,win: handle zero-sized allocations uniformly `malloc(0)` and `realloc(p, 0)` can either return NULL or a unique pointer. Make our custom allocator return NULL for consistency across platforms and libcs. PR-URL: https://github.com/libuv/libuv/pull/2038 Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: Richard Lau Reviewed-By: Sakthipriyan Vairamani Reviewed-By: Santiago Gimeno --- src/uv-common.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/uv-common.c b/src/uv-common.c index f0aec452..71d100a1 100644 --- a/src/uv-common.c +++ b/src/uv-common.c @@ -72,7 +72,9 @@ char* uv__strndup(const char* s, size_t n) { } void* uv__malloc(size_t size) { - return uv__allocator.local_malloc(size); + if (size > 0) + return uv__allocator.local_malloc(size); + return NULL; } void uv__free(void* ptr) { @@ -91,7 +93,10 @@ void* uv__calloc(size_t count, size_t size) { } void* uv__realloc(void* ptr, size_t size) { - return uv__allocator.local_realloc(ptr, size); + if (size > 0) + return uv__allocator.local_realloc(ptr, size); + uv__free(ptr); + return NULL; } int uv_replace_allocator(uv_malloc_func malloc_func,