From 017da44ac2afb084d95f28645d956dbcc79ca214 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Thu, 6 May 2021 20:35:00 +0200 Subject: [PATCH] LibC: Make malloc(0) return a non-null pointer Legally we could just return a null pointer, however returning a pointer other than the null pointer is more compatible with improperly written software that assumes that a null pointer means allocation failure. --- Userland/Libraries/LibC/malloc.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibC/malloc.cpp b/Userland/Libraries/LibC/malloc.cpp index 416a4b659bb..bf459c71254 100644 --- a/Userland/Libraries/LibC/malloc.cpp +++ b/Userland/Libraries/LibC/malloc.cpp @@ -162,8 +162,11 @@ static void* malloc_impl(size_t size, CallerWillInitializeMemory caller_will_ini if (s_log_malloc) dbgln("LibC: malloc({})", size); - if (!size) - return nullptr; + if (!size) { + // Legally we could just return a null pointer here, but this is more + // compatible with existing software. + size = 1; + } g_malloc_stats.number_of_malloc_calls++;