Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add assertion to ensure Java ByteBuffers are direct #5

Merged
merged 1 commit into from
Jul 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion support-lib/cpp/DataRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ class DataRefJNI : public DataRef::Impl {
// call ByteBuffer.isReadOnly() to determine mutability
_readonly = env->CallBooleanMethod(_data.get(), ::djinni::JniClass<BufferClassInfo>::get().isReadOnly) != 0;
::djinni::jniExceptionCheck(env);
_len = static_cast<size_t>(env->GetDirectBufferCapacity(_data.get()));
auto len = env->GetDirectBufferCapacity(_data.get());
assert(len != -1); // GetDirectBufferCapacity() returns -1 when the ByteBuffer is not direct
_len = static_cast<size_t>(len);
_buf = reinterpret_cast<uint8_t*>(env->GetDirectBufferAddress(_data.get()));
}
// take over a std::vector's buffer without copying it
Expand Down
4 changes: 3 additions & 1 deletion support-lib/jni/DataView_jni.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ struct NativeDataView {
using JniType = jobject;

static CppType toCpp(JNIEnv* jniEnv, JniType o) {
return DataView((uint8_t*)jniEnv->GetDirectBufferAddress(o), (size_t)jniEnv->GetDirectBufferCapacity(o));
auto size = jniEnv->GetDirectBufferCapacity(o);
assert(size != -1); // GetDirectBufferCapacity() returns -1 when the ByteBuffer is not direct
return DataView(reinterpret_cast<uint8_t*>(jniEnv->GetDirectBufferAddress(o)), static_cast<size_t>(size));
}

static ::djinni::LocalRef<JniType> fromCpp(JNIEnv* jniEnv, const CppType& c) {
Expand Down