From e1f0258f8e639f4f5985f70e10f160231db26de4 Mon Sep 17 00:00:00 2001 From: Gleb Koval Date: Sun, 24 Nov 2024 15:09:32 +0000 Subject: [PATCH] fix: handle malloc result in init_process_result --- src/threads/thread.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/threads/thread.c b/src/threads/thread.c index 90a2a8b..91a12b5 100644 --- a/src/threads/thread.c +++ b/src/threads/thread.c @@ -71,7 +71,7 @@ static void kernel_thread (thread_func *, void *aux); static void idle (void *aux UNUSED); static struct thread *running_thread (void); static struct thread *next_thread_to_run (void); -static void init_process_result (struct thread *t); +static bool init_process_result (struct thread *t); static void init_thread (struct thread *, const char *name, int nice, int priority, fp32_t recent_cpu); static bool is_thread (struct thread *) UNUSED; @@ -252,7 +252,11 @@ thread_create (const char *name, int priority, struct thread *parent_thread = thread_current (); init_thread (t, name, parent_thread->nice, priority, parent_thread->recent_cpu); tid = t->tid = allocate_tid (); - init_process_result (t); + if (!init_process_result (t)) + { + palloc_free_page (t); + return TID_ERROR; + } #ifdef USERPROG /* Initialize the thread's file descriptor table. */ @@ -668,15 +672,18 @@ is_thread (struct thread *t) } /* Allocate and initialise a process result for given thread. */ -static void +static bool init_process_result (struct thread *t) { struct process_result *result = malloc (sizeof (struct process_result)); + if (result == NULL) + return false; result->tid = t->tid; result->exit_status = -1; lock_init (&result->lock); sema_init (&result->sema, 0); t->result = result; + return true; } /* Does basic initialization of T as a blocked thread named