From b847541ee8566d5d90af6b49476a32351e2b11d6 Mon Sep 17 00:00:00 2001 From: Daniel Bertalan Date: Mon, 12 Jul 2021 19:17:41 +0200 Subject: [PATCH] Kernel: Allow passing null pointer to delete The C++ standard says that it's legal to call the `delete` operator with a null pointer argument, in which case it should be a no-op. I encountered this issue when running a kernel that's compiled with Clang. I assume this fact was used for some kind of optimization. --- Kernel/Heap/SlabAllocator.h | 2 ++ Kernel/KString.cpp | 2 ++ 2 files changed, 4 insertions(+) diff --git a/Kernel/Heap/SlabAllocator.h b/Kernel/Heap/SlabAllocator.h index bfaa2c99e1..5650d428c1 100644 --- a/Kernel/Heap/SlabAllocator.h +++ b/Kernel/Heap/SlabAllocator.h @@ -33,6 +33,8 @@ public: \ } \ void operator delete(void* ptr) noexcept \ { \ + if (!ptr) \ + return; \ slab_dealloc(ptr, sizeof(type)); \ } \ \ diff --git a/Kernel/KString.cpp b/Kernel/KString.cpp index 201554938d..da5c3bf76c 100644 --- a/Kernel/KString.cpp +++ b/Kernel/KString.cpp @@ -59,6 +59,8 @@ OwnPtr KString::try_clone() const void KString::operator delete(void* string) { + if (!string) + return; size_t allocation_size = sizeof(KString) + (sizeof(char) * static_cast(string)->m_length) + sizeof(char); kfree_sized(string, allocation_size); }