Skip to content

Commit

Permalink
Merge pull request #1378 from DaveCTurner/2021-08-30-ffi_closure_allo…
Browse files Browse the repository at this point in the history
…c-returns-NULL

Handle failure in `ffi_closure_alloc`
  • Loading branch information
matthiasblaesing committed Sep 14, 2021
2 parents 030411b + 3f5e937 commit 4ebb64a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -11,6 +11,7 @@ Features

Bug Fixes
---------
* [#1378](https://github.com/java-native-access/jna/pull/1378): Handle failure in `ffi_closure_alloc` - [@davecturner](https://github.com/davecturner).


Release 5.9.0
Expand Down
20 changes: 20 additions & 0 deletions native/dispatch.c
Expand Up @@ -3466,6 +3466,11 @@ Java_com_sun_jna_Native_registerMethod(JNIEnv *env, jclass UNUSED(ncls),
}

closure = ffi_closure_alloc(sizeof(ffi_closure), &code);
if (closure == NULL) {
throwByName(env, EUnsupportedOperation, "Failed to allocate closure");
status = FFI_BAD_ABI;
goto cleanup;
}
status = ffi_prep_closure_loc(closure, closure_cif, dispatch_direct, data, code);
if (status != FFI_OK) {
throwByName(env, EError, "Native method linkage failed");
Expand Down Expand Up @@ -3514,16 +3519,31 @@ 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);
if (cb->object == NULL) {
// either obj was null or an OutOfMemoryError has been thrown
free(cb);
return 0;
}
cb->closure = ffi_closure_alloc(sizeof(ffi_closure), L2A(&cb->x_closure));
if (cb->closure == NULL) {
(*env)->DeleteWeakGlobalRef(env, cb->object);
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);
(*env)->DeleteWeakGlobalRef(env, cb->object);
free(cb);
return 0;
}
return A2L(cb);
Expand Down

0 comments on commit 4ebb64a

Please sign in to comment.