From b466ede1eab7c803674d8375e193b87a0e6f936b Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 7 Feb 2021 18:13:51 +0100 Subject: Kernel: Make sure we can allocate kernel stack before creating thread Wrap thread creation in a Thread::try_create() helper that first allocates a kernel stack region. If that allocation fails, we propagate an ENOMEM error to the caller. This avoids the situation where a thread is half-constructed, without a valid kernel stack, and avoids having to do messy cleanup in that case. --- Kernel/Syscalls/thread.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'Kernel/Syscalls/thread.cpp') diff --git a/Kernel/Syscalls/thread.cpp b/Kernel/Syscalls/thread.cpp index 5f975c8ffd..d65591833a 100644 --- a/Kernel/Syscalls/thread.cpp +++ b/Kernel/Syscalls/thread.cpp @@ -60,11 +60,11 @@ int Process::sys$create_thread(void* (*entry)(void*), Userspacewas_created()) { - // Could not fully create a thread - return -ENOMEM; - } + auto thread_or_error = Thread::try_create(*this); + if (thread_or_error.is_error()) + return thread_or_error.error(); + + auto& thread = thread_or_error.value(); // We know this thread is not the main_thread, // So give it a unique name until the user calls $set_thread_name on it -- cgit v1.2.3