From 13d1c8df57a9467b608776db52853efeadbc8f64 Mon Sep 17 00:00:00 2001 From: afrangio Date: Wed, 4 May 2022 19:51:58 +0200 Subject: [PATCH] malloc: oops if you try to allocate size 0 --- src/memory/malloc.rs | 12 ++++++++++++ src/tty/basic_commands.rs | 5 ++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/memory/malloc.rs b/src/memory/malloc.rs index bdd6058..b4a197c 100644 --- a/src/memory/malloc.rs +++ b/src/memory/malloc.rs @@ -18,6 +18,12 @@ struct AllocHeader pub fn kmalloc(size: usize) -> *mut c_void { + if size == 0 + { + crate::oops!("cannot allocate memory of size 0"); + return core::ptr::null_mut::(); + } + let size = ferramenta::align(size, 0x10); let pt_manager: &mut pagetable::Manager = unsafe { @@ -63,6 +69,12 @@ pub fn ksize(address: *mut c_void) -> usize pub fn vmalloc(size: usize) -> *mut c_void { + if size == 0 + { + crate::oops!("cannot allocate memory of size 0"); + return core::ptr::null_mut::(); + } + let size = ferramenta::align(size, 0x10); let pt_manager: &mut pagetable::Manager = unsafe { diff --git a/src/tty/basic_commands.rs b/src/tty/basic_commands.rs index a6a2ba2..797e914 100644 --- a/src/tty/basic_commands.rs +++ b/src/tty/basic_commands.rs @@ -140,7 +140,10 @@ fn allocate(size: usize, kernel_space: bool) { memory::vmalloc(size) }; - + if address.is_null() + { + return; + } let size = if kernel_space { memory::ksize(address)