Skip to content

Commit

Permalink
<fix>(jni): fix jni method not handle jstring or jbyteArray null poin…
Browse files Browse the repository at this point in the history
…t coredump. (#187)
  • Loading branch information
kyonRay committed Oct 31, 2023
1 parent 0333ad8 commit f9de0c4
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 52 deletions.
2 changes: 1 addition & 1 deletion bindings/java/jni/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ dependencies {

archivesBaseName = 'bcos-sdk-jni'
group = 'org.fisco-bcos'
version = '3.5.0'
version = '3.5.1-SNAPSHOT'

// Additional attribute definition
ext {
Expand Down
17 changes: 17 additions & 0 deletions bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ jclass bcos_sdk_c_find_jclass(JNIEnv* env, const char* className);

struct bcos_sdk_c_config* create_config_from_java_obj(JNIEnv* env, jobject jconfig);

// check jstring not null
inline void checkJString(JNIEnv* env, jstring jstr)
{
if (jstr == NULL)
{
THROW_JNI_EXCEPTION(env, "illegal NULL string parameter");
}
}
// check jByteArray not null
inline void checkJByteArray(JNIEnv* env, jbyteArray jbyte_array)
{
if (jbyte_array == NULL)
{
THROW_JNI_EXCEPTION(env, "illegal NULL byteArray parameter");
}
}

#ifdef __cplusplus
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ static void on_receive_block_notifier(const char* group, int64_t block_number, v
JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_BcosSDKJniObj_registerBlockNotifier(
JNIEnv* env, jobject self, jstring jgroup, jobject jcallback)
{
checkJString(env, jgroup);

void* sdk = bcos_sdk_get_native_pointer(env, self);
if (!sdk)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ JNIEXPORT void JNICALL
Java_org_fisco_bcos_sdk_jni_amop_AmopJniObj_subscribeTopic__Ljava_lang_String_2Lorg_fisco_bcos_sdk_jni_amop_AmopRequestCallback_2(
JNIEnv* env, jobject self, jstring jtopic, jobject jcallback)
{
checkJString(env, jtopic);

void* sdk = bcos_sdk_get_native_pointer(env, self);

const char* topic = env->GetStringUTFChars(jtopic, 0);
Expand Down Expand Up @@ -261,6 +263,7 @@ JNIEXPORT void JNICALL
Java_org_fisco_bcos_sdk_jni_amop_AmopJniObj_subscribeTopic__Ljava_lang_String_2Lorg_fisco_bcos_sdk_jni_amop_AmopJniObjRequestCallback_2(
JNIEnv* env, jobject self, jstring jtopic, jobject jcallback)
{
checkJString(env, jtopic);
void* sdk = bcos_sdk_get_native_pointer(env, self);

const char* topic = env->GetStringUTFChars(jtopic, 0);
Expand Down Expand Up @@ -363,6 +366,8 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_amop_AmopJniObj_setCallback(
JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_amop_AmopJniObj_sendAmopMsg(
JNIEnv* env, jobject self, jstring jtopic, jbyteArray jdata, jint jtimeout, jobject jcallback)
{
checkJString(env, jtopic);
checkJByteArray(env, jdata);
void* sdk = bcos_sdk_get_native_pointer(env, self);

// Note: The JNIEnv pointer, passed as the first argument to every native method, can only be
Expand Down Expand Up @@ -399,6 +404,8 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_amop_AmopJniObj_sendAmopMsg(
JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_amop_AmopJniObj_broadcastAmopMsg(
JNIEnv* env, jobject self, jstring jtopic, jbyteArray jdata)
{
checkJString(env, jtopic);
checkJByteArray(env, jdata);
void* sdk = bcos_sdk_get_native_pointer(env, self);

const char* topic = env->GetStringUTFChars(jtopic, 0);
Expand All @@ -419,6 +426,9 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_amop_AmopJniObj_broadcastAmop
JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_amop_AmopJniObj_sendResponse(
JNIEnv* env, jobject self, jstring jendpoint, jstring jseq, jbyteArray jdata)
{
checkJString(env, jendpoint);
checkJString(env, jseq);
checkJByteArray(env, jdata);
void* sdk = bcos_sdk_get_native_pointer(env, self);

const char* endpoint = env->GetStringUTFChars(jendpoint, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_event_EventSubJniObj_stop(
JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_event_EventSubJniObj_subscribeEvent(
JNIEnv* env, jobject self, jstring jgroup, jstring jparams, jobject jcallback)
{
checkJString(env, jgroup);
checkJString(env, jparams);

void* sdk = bcos_sdk_get_native_pointer(env, self);
const char* group = env->GetStringUTFChars(jgroup, 0);
const char* params = env->GetStringUTFChars(jparams, 0);
Expand Down Expand Up @@ -168,6 +171,7 @@ JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_event_EventSubJniObj_subsc
JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_event_EventSubJniObj_unsubscribeEvent(
JNIEnv* env, jobject self, jstring jeventId)
{
checkJString(env, jeventId);
void* sdk = bcos_sdk_get_native_pointer(env, self);
const char* eventid = env->GetStringUTFChars(jeventId, 0);

Expand Down
Loading

0 comments on commit f9de0c4

Please sign in to comment.