Skip to content

Commit

Permalink
We need to use NewGloblRef when caching jclass instances (#9595)
Browse files Browse the repository at this point in the history
Motivation:

It is not safe to cache a jclass without obtaining a global reference via NewGlobalRef.

Modifications:

Correctly use NewGlobalRef(...) before caching

Result:

Correctly cache jclass instance
  • Loading branch information
normanmaurer committed Sep 23, 2019
1 parent 4499384 commit dc4de7f
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions transport-native-kqueue/src/main/c/netty_kqueue_bsdsocket.c
Expand Up @@ -33,7 +33,7 @@
#include "netty_unix_util.h"

// Those are initialized in the init(...) method and cached for performance reasons
static jclass stringCls = NULL;
static jclass stringClass = NULL;
static jclass peerCredentialsClass = NULL;
static jfieldID fileChannelFieldId = NULL;
static jfieldID transferredFieldId = NULL;
Expand Down Expand Up @@ -108,7 +108,7 @@ static jobjectArray netty_kqueue_bsdsocket_getAcceptFilter(JNIEnv* env, jclass c
netty_unix_errors_throwChannelExceptionErrorNo(env, "getsockopt() failed: ", errno);
return NULL;
}
jobjectArray resultArray = (*env)->NewObjectArray(env, 2, stringCls, NULL);
jobjectArray resultArray = (*env)->NewObjectArray(env, 2, stringClass, NULL);
(*env)->SetObjectArrayElement(env, resultArray, 0, (*env)->NewStringUTF(env, &af.af_name[0]));
(*env)->SetObjectArrayElement(env, resultArray, 1, (*env)->NewStringUTF(env, &af.af_arg[0]));
return resultArray;
Expand Down Expand Up @@ -272,11 +272,16 @@ jint netty_kqueue_bsdsocket_JNI_OnLoad(JNIEnv* env, const char* packagePrefix) {
netty_unix_errors_throwRuntimeException(env, "failed to get field ID: FileDescriptor.fd");
return JNI_ERR;
}
stringCls = (*env)->FindClass(env, "java/lang/String");
jclass stringCls = (*env)->FindClass(env, "java/lang/String");
if (stringCls == NULL) {
// pending exception...
return JNI_ERR;
}
if ((stringClass = (*env)->NewGlobalRef(env, stringCls)) == NULL) {
// out-of-memory!
netty_unix_errors_throwOutOfMemoryError(env);
return JNI_ERR;
}

nettyClassName = netty_unix_util_prepend(packagePrefix, "io/netty/channel/unix/PeerCredentials");
jclass localPeerCredsClass = (*env)->FindClass(env, nettyClassName);
Expand Down Expand Up @@ -306,4 +311,8 @@ void netty_kqueue_bsdsocket_JNI_OnUnLoad(JNIEnv* env) {
(*env)->DeleteGlobalRef(env, peerCredentialsClass);
peerCredentialsClass = NULL;
}
if (stringClass != NULL) {
(*env)->DeleteGlobalRef(env, stringClass);
stringClass = NULL;
}
}

0 comments on commit dc4de7f

Please sign in to comment.