Skip to content

Commit

Permalink
Explicitly cast the callback and userData pointers to jlong to safely…
Browse files Browse the repository at this point in the history
… pass them to the Java code:

- on win32 pointers are 4 bytes
- reinterpret_cast win32_ptr to jlong type allows us to safely pass it from native -> Java -> to native
  • Loading branch information
Montura committed Jul 11, 2024
1 parent 0ccf681 commit a1f2e4a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/main/c/jni-lib/src/listeners/DxEventListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ namespace dxfeed {
DxEventListener* DxEventListener::create(JNIEnv* env, dxfg_feed_event_listener_function callback, void* userData) {
auto dxSubscriptionJniClass = jni::safeFindClass(env, "com/dxfeed/api/DxSubscriptionJni");
auto newEventListenerId = jni::safeGetStaticMethodID(env, dxSubscriptionJniClass, "newEventListener", "(JJ)J");
auto result = jni::checkedCallStaticLongMethod(env, dxSubscriptionJniClass, newEventListenerId, callback, userData);
auto pCallback = dxfeed::r_cast<jlong>(callback);
auto pUserData = dxfeed::r_cast<jlong>(userData);
auto result = jni::checkedCallStaticLongMethod(env, dxSubscriptionJniClass, newEventListenerId, pCallback, pUserData);
env->DeleteLocalRef(dxSubscriptionJniClass);
return new DxEventListener(result);
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/c/jni-lib/src/listeners/DxStateChangeListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ namespace dxfeed {
auto dxEndpointClass = jni::safeFindClass(env, "com/dxfeed/api/DxEndpointJni");
auto newStateChangeListenerId =
jni::safeGetStaticMethodID(env, dxEndpointClass, "newStateChangeEventListener", "(JJ)J");
auto result = jni::checkedCallStaticLongMethod(env, dxEndpointClass, newStateChangeListenerId, callback, userData);
auto pCallback = dxfeed::r_cast<jlong>(callback);
auto pUserData = dxfeed::r_cast<jlong>(userData);
auto result = jni::checkedCallStaticLongMethod(env, dxEndpointClass, newStateChangeListenerId, pCallback, pUserData);
env->DeleteLocalRef(dxEndpointClass);
return new DxStateChangeListener(result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ namespace dxfeed {
auto dxEndpointClass = jni::safeFindClass(env, "com/dxfeed/api/DxSubscriptionJni");
auto newChangeListenerId =
jni::safeGetStaticMethodID(env, dxEndpointClass, "newChangeListener", "(JJJJ)J");
auto pOnSymbolsAdded = dxfeed::r_cast<jlong>(fSymbolsAdded);
auto pOnSymbolsRemoved = dxfeed::r_cast<jlong>(fSymbolsRemoved);
auto pOnSubscriptionClosed = dxfeed::r_cast<jlong>(fSubscriptionClosed);
auto pUserData = dxfeed::r_cast<jlong>(userData);
auto result = jni::checkedCallStaticLongMethod(env, dxEndpointClass, newChangeListenerId,
fSymbolsAdded, fSymbolsRemoved, fSubscriptionClosed, userData);
pOnSymbolsAdded, pOnSymbolsRemoved, pOnSubscriptionClosed, pUserData);
env->DeleteLocalRef(dxEndpointClass);
return new DxSubscriptionChangeListener(result);
}
Expand Down

0 comments on commit a1f2e4a

Please sign in to comment.