linux-user: Fix a memory leak when pthread_create fails
Fix one of the TODO items when creating a new thread: release the copied cpu and free the task state. Signed-off-by: Warner Losh <imp@bsdimp.com> Reviewed-by: Helge Deller <deller@gmx.de> Signed-off-by: Helge Deller <deller@gmx.de>
Warner Losh committed
May 7, 2026 at 13:24 UTC
1e000f9671aed33a3f86d2fdc61f991424628bb4
1 file changed
+9
-1
linux-user/syscall.c
+9
-1
@@ -7005,7 +7005,6 @@ static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
7005
cpu->random_seed = qemu_guest_random_seed_thread_part1();
7006
7007
ret = pthread_create(&info.thread, &attr, clone_func, &info);
7008
- /* TODO: Free new CPU state if thread creation failed. */
7008
7009
sigprocmask(SIG_SETMASK, &info.sigmask, NULL);
7010
pthread_attr_destroy(&attr);
@@ -7014,7 +7013,16 @@ static int do_fork(CPUArchState *env, unsigned int flags, abi_ulong newsp,
7013
pthread_cond_wait(&info.cond, &info.mutex);
7014
ret = info.tid;
7015
} else {
7016
+ errno = ret;
7017
ret = -1;
7018
+ object_unparent(OBJECT(new_cpu));
7019
+ object_unref(OBJECT(new_cpu));
7020
+#ifdef TARGET_AARCH64
7021
+ if (ts->gcs_base) {
7022
+ target_munmap(ts->gcs_base, ts->gcs_size);
7023
+ }
7024
+#endif
7025
+ g_free(ts);
7026
}
7027
pthread_mutex_unlock(&info.mutex);
7028
pthread_cond_destroy(&info.cond);