From 45229810be5cb5a6a1adfae10166c78f01bcb464 Mon Sep 17 00:00:00 2001 From: "Enrico Fraccaroli (Galfurian)" Date: Thu, 29 Feb 2024 11:17:09 -0500 Subject: [PATCH] - Check for errors from the brk; - Use the new malloc_header_to_ptr function in malloc; --- libc/src/stdlib.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/libc/src/stdlib.c b/libc/src/stdlib.c index f28a31a..1005bb0 100644 --- a/libc/src/stdlib.c +++ b/libc/src/stdlib.c @@ -44,17 +44,19 @@ void *malloc(unsigned int size) if (size == 0) { return NULL; } - size_t *ptr; - // Compute the real size we need to allocate. - size_t real_size = size + sizeof(malloc_header_t); + void *ptr; // Allocate the memory. - __inline_syscall1(ptr, brk, real_size); + __inline_syscall1(ptr, brk, size + sizeof(malloc_header_t)); + // Check for errors from the brk. + if (ptr == 0) { + return NULL; + } // Initialize the malloc header. - malloc_header_t *malloc_header = (malloc_header_t *)ptr; - malloc_header->magic = MALLOC_MAGIC_NUMBER; - malloc_header->size = size; + malloc_header_t *header = (malloc_header_t *)ptr; + header->magic = MALLOC_MAGIC_NUMBER; + header->size = size; // Return the allocated memory. - return (void *)((char *)ptr + sizeof(malloc_header_t)); + return malloc_header_to_ptr(header); } void *calloc(size_t num, size_t size)