Skip to content

Commit

Permalink
Handle failures in Native#ffi_prep_closure too
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveCTurner committed Sep 6, 2021
1 parent a40db1b commit bb75249
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions native/dispatch.c
Expand Up @@ -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);
Expand Down

0 comments on commit bb75249

Please sign in to comment.