From 5e166f3042a8e7b3031aae4da7006f80caa53ecc Mon Sep 17 00:00:00 2001 From: Danny Holman Date: Wed, 26 Jun 2024 00:01:23 -0500 Subject: arch: i386: kmalloc: fix last element being ignored Fix a bug in kmalloc in which the last element of the mem_block linked list would be ignored and potentially overwritten. Signed-off-by: Danny Holman --- arch/i386/kernel/kmalloc.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/arch/i386/kernel/kmalloc.c b/arch/i386/kernel/kmalloc.c index 9c82e39..ea77428 100644 --- a/arch/i386/kernel/kmalloc.c +++ b/arch/i386/kernel/kmalloc.c @@ -37,9 +37,9 @@ void* kmalloc(size_t sz) { struct mem_block *temp = first; while (temp->next != NULL) { - if (temp->size <= sz && temp->alloc == 0) { - temp->alloc = 1; - return (void*)temp->start; + if (temp->next->size <= sz && temp->next->alloc == 0) { + temp->next->alloc = 1; + return (void*)temp->next->start; } temp = temp->next; } @@ -48,11 +48,10 @@ void* kmalloc(size_t sz) { if (temp->next > kbrk) _request_page(); temp->next->prev = temp; - temp = temp->next; - temp->start = (uintptr_t)(temp + sizeof(struct mem_block)); - temp->size = sz; - temp->alloc = 1; - temp->next = NULL; + temp->next->start = (uintptr_t)(temp->next + sizeof(struct mem_block)); + temp->next->size = sz; + temp->next->alloc = 1; + temp->next->next = NULL; return (void*)temp->start; } -- cgit v1.2.3