From 2dc68a33ab2ec8a28d1302f080b78fdf85c0f289 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Sun, 31 May 2026 17:23:12 +0100 Subject: [PATCH] libc: malloc: fix heap_expand being called with a size measured in pages rather than bytes --- lib/libc/malloc/liballoc.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/libc/malloc/liballoc.c b/lib/libc/malloc/liballoc.c index b9b46c6..948dac4 100644 --- a/lib/libc/malloc/liballoc.c +++ b/lib/libc/malloc/liballoc.c @@ -1,5 +1,10 @@ #include "liballoc.h" +#include +#include +#include +#include + /** Durand's Amazing Super Duper Memory functions. */ #define VERSION "1.1" @@ -206,6 +211,8 @@ static struct liballoc_major *allocate_new_page(heap_t *heap, unsigned int size) return NULL; // uh oh, we ran out of memory. } + memset(maj, 0x0, sizeof *maj); + maj->prev = NULL; maj->next = NULL; maj->pages = st; @@ -455,13 +462,11 @@ void *PREFIX(malloc)(heap_t *heap, size_t req_size) if (diff >= (size + sizeof(struct liballoc_minor))) { // yay.... - min->next - = (struct liballoc_minor - *)((uintptr_t)min - + sizeof( - struct - liballoc_minor) - + min->size); + min->next = (struct liballoc_minor + *)((uintptr_t)min + + sizeof( + struct liballoc_minor) + + min->size); min->next->prev = min; min = min->next; min->next = NULL; @@ -506,8 +511,7 @@ void *PREFIX(malloc)(heap_t *heap, size_t req_size) new_min = (struct liballoc_minor *)((uintptr_t)min + sizeof( - struct - liballoc_minor) + struct liballoc_minor) + min->size); new_min->magic = LIBALLOC_MAGIC; @@ -822,9 +826,11 @@ int liballoc_unlock(heap_t *heap) return 0; } -void *liballoc_alloc(heap_t *heap, size_t sz) +void *liballoc_alloc(heap_t *heap, size_t nr_pages) { - return heap_expand(heap, sz); + size_t page_size = 0; + kern_config_get(KERN_CFG_PAGE_SIZE, &page_size, sizeof page_size); + return heap_expand(heap, nr_pages * page_size); } int liballoc_free(heap_t *heap, void *p, size_t sz)