diff mbox series

[40/66] bsd-user: Free CPU state and TaskState on thread creation failure

Message ID 20260515-misc-2026q2-v1-40-5438ca41b27a@bsdimp.com
State New
Headers show
Series bsd-user: Upstream most of the remaining system calls | expand

Commit Message

Warner Losh May 15, 2026, 9:19 p.m. UTC
When pthread_create fails in do_freebsd_thr_new, the allocated
TaskState and CPU created by cpu_copy were leaked. Clean them up
using the same object_unparent/object_unref pattern used by
thr_exit, and free the TaskState with g_free.

Signed-off-by: Warner Losh <imp@bsdimp.com>
---
 bsd-user/freebsd/os-thread.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/bsd-user/freebsd/os-thread.c b/bsd-user/freebsd/os-thread.c
index 1d88ee05e4..9e6055a040 100644
--- a/bsd-user/freebsd/os-thread.c
+++ b/bsd-user/freebsd/os-thread.c
@@ -1633,7 +1633,6 @@  abi_long do_freebsd_thr_new(CPUArchState *env,
     sigprocmask(SIG_BLOCK, &sigmask, &info.sigmask);
 
     ret = pthread_create(&info.thread, &attr, new_freebsd_thread_start, &info);
-    /* XXX Free new CPU state if thread creation fails. */
 
     sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
     pthread_attr_destroy(&attr);
@@ -1642,6 +1641,9 @@  abi_long do_freebsd_thr_new(CPUArchState *env,
         pthread_cond_wait(&info.cond, &info.mutex);
     } else {
         /* Creation of new thread failed. */
+        object_unparent(OBJECT(new_cpu));
+        object_unref(OBJECT(new_cpu));
+        g_free(ts);
         ret = -host_to_target_errno(errno);
     }