summaryrefslogtreecommitdiff
path: root/Kernel/Syscalls/thread.cpp
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-02-07 18:13:51 +0100
committerAndreas Kling <kling@serenityos.org>2021-02-07 19:27:00 +0100
commitb466ede1eab7c803674d8375e193b87a0e6f936b (patch)
treede199042db507d52555c9c235c07a26f7794c30f /Kernel/Syscalls/thread.cpp
parent5c45b0d32d79d5070442e7bb2ced57c8a0c4ae73 (diff)
downloadserenity-b466ede1eab7c803674d8375e193b87a0e6f936b.zip
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.
Diffstat (limited to 'Kernel/Syscalls/thread.cpp')
-rw-r--r--Kernel/Syscalls/thread.cpp10
1 files changed, 5 insertions, 5 deletions
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*), Userspace<const Syscall::S
// FIXME: Do something with guard pages?
- auto thread = adopt(*new Thread(*this));
- if (!thread->was_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