From e8a401efc21e44981457baaf52360fd6f975b8a7 Mon Sep 17 00:00:00 2001 From: apio Date: Fri, 10 Mar 2023 21:02:09 +0100 Subject: [PATCH] libluna/Heap: Crash the kernel (but not userspace) on invalid frees This makes them way easier to catch and forces us to get those out of the way. --- libluna/src/Heap.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/libluna/src/Heap.cpp b/libluna/src/Heap.cpp index 8c7e9fdf..c989eaec 100644 --- a/libluna/src/Heap.cpp +++ b/libluna/src/Heap.cpp @@ -249,13 +249,21 @@ Result free_impl(void* ptr) else dbgln("ERROR: Attempt to free memory at %p, which wasn't allocated with malloc", ptr); +#ifdef USE_FREESTANDING + fail("Call to free_impl() with an invalid argument (double-free or erroneous deallocation)"); +#else return err(EFAULT); +#endif } if (is_block_free(block)) { dbgln("ERROR: Attempt to free memory at %p, which was already freed", ptr); +#ifdef USE_FREESTANDING + fail("Call to free_impl() with a pointer to freed memory (probably double-free)"); +#else return err(EFAULT); +#endif } else block->status &= ~BLOCK_USED;