Fix thread.posix.c

- Fix memory leak when thread_spawn fails
- Remove incorrect assertion (pthread_t in general case is neither scalar
  nor non-zero)
This commit is contained in:
Vftdan 2024-10-24 10:23:51 +02:00
parent 4eed5f9af1
commit 67fb39b808
1 changed files with 3 additions and 2 deletions

View File

@ -76,14 +76,15 @@ thread_spawn(ThreadEntry entry, ThreadResult error_result)
pthread_t handle; pthread_t handle;
int error; int error;
if (!new_uninitialized(&th)) { if (!new_uninitialized(&th)) {
free(native_arg);
return THREAD_NONE; return THREAD_NONE;
} }
error = pthread_create(&handle, NULL, &run_entry, native_arg); error = pthread_create(&handle, NULL, &run_entry, native_arg); // calls free(native_arg) if error == 0
if (error) { if (error) {
errno = error; errno = error;
free(native_arg);
return delete_handle(th); return delete_handle(th);
} }
assert(handle && "System allows 0 as thread id");
initialize_handle(&th, handle); initialize_handle(&th, handle);
return th; return th;
} }