Skip to content

Commit a1dfc3c

Browse files
committed
wasi_threads.c: plug a few leaks
1 parent 7c55fe4 commit a1dfc3c

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

lib/wasi_threads.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ runner(void *vp)
226226
goto fail;
227227
}
228228
fail:
229+
instance_destroy(inst);
229230
toywasm_mutex_lock(&wasi->lock);
230231
idalloc_free(&wasi->tids, tid);
231232
assert(wasi->nrunners > 0);
@@ -235,7 +236,6 @@ runner(void *vp)
235236
assert(ret == 0);
236237
}
237238
toywasm_mutex_unlock(&wasi->lock);
238-
instance_destroy(inst);
239239
return NULL;
240240
}
241241

@@ -248,6 +248,7 @@ wasi_thread_spawn(struct exec_context *ctx, struct host_instance *hi,
248248
struct wasi_threads_instance *wasi = (void *)hi;
249249
HOST_FUNC_CONVERT_PARAMS(ft, params);
250250
uint32_t user_arg = HOST_FUNC_PARAM(ft, params, 0, i32);
251+
struct instance *inst = NULL;
251252
struct thread_arg *arg = NULL;
252253
uint32_t tid;
253254
int ret;
@@ -272,7 +273,6 @@ wasi_thread_spawn(struct exec_context *ctx, struct host_instance *hi,
272273
goto fail;
273274
}
274275

275-
struct instance *inst;
276276
struct report report;
277277
report_init(&report);
278278
ret = instance_create(wasi->module, &inst, ctx->instance,
@@ -308,6 +308,7 @@ wasi_thread_spawn(struct exec_context *ctx, struct host_instance *hi,
308308
xlog_trace("%s: pthread_create failed with %d", __func__, ret);
309309
goto fail;
310310
}
311+
inst = NULL;
311312
arg = NULL;
312313

313314
ret = pthread_detach(t);
@@ -323,6 +324,9 @@ wasi_thread_spawn(struct exec_context *ctx, struct host_instance *hi,
323324
toywasm_mutex_unlock(&wasi->lock);
324325

325326
fail:
327+
if (inst != NULL) {
328+
instance_destroy(inst);
329+
}
326330
free(arg);
327331
int32_t result;
328332
if (ret != 0) {

0 commit comments

Comments
 (0)