From bb75249a2f5f17353260b563745b0af67f1a08e8 Mon Sep 17 00:00:00 2001 From: David Turner Date: Mon, 6 Sep 2021 08:05:18 +0100 Subject: [PATCH] Handle failures in Native#ffi_prep_closure too --- native/dispatch.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/native/dispatch.c b/native/dispatch.c index 949856bcb..345a7c2ad 100644 --- a/native/dispatch.c +++ b/native/dispatch.c @@ -3519,16 +3519,24 @@ Java_com_sun_jna_Native_ffi_1prep_1closure(JNIEnv *env, jclass UNUSED(cls), jlon ffi_status s; if ((*env)->GetJavaVM(env, &cb->vm) != JNI_OK) { + free(cb); throwByName(env, EUnsatisfiedLink, "Can't get Java VM"); return 0; } cb->object = (*env)->NewWeakGlobalRef(env, obj); cb->closure = ffi_closure_alloc(sizeof(ffi_closure), L2A(&cb->x_closure)); + if (cb->closure == NULL) { + free(cb); + throwByName(env, EUnsupportedOperation, "Failed to allocate closure"); + return 0; + } s = ffi_prep_closure_loc(cb->closure, L2A(cif), &closure_handler, cb, cb->x_closure); if (ffi_error(env, "ffi_prep_cif", s)) { + ffi_closure_free(cb->closure); + free(cb); return 0; } return A2L(cb);